RoomMemberModel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Models\Goodnight;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\User\UserModel;
  5. /**
  6. * App\Models\Goodnight\RoomMemberModel
  7. *
  8. * @property int $id
  9. * @property int $uid
  10. * @property int $room_id 房间ID
  11. * @property string $day1_voice
  12. * @property string $day2_voice
  13. * @property string $day3_voice
  14. * @property \Illuminate\Support\Carbon $created_at
  15. * @property \Illuminate\Support\Carbon $updated_at
  16. * @property-read \App\Models\User\UserModel $user
  17. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel newModelQuery()
  18. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel newQuery()
  19. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel query()
  20. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel whereCreatedAt($value)
  21. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel whereDay1Voice($value)
  22. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel whereDay2Voice($value)
  23. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel whereDay3Voice($value)
  24. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel whereId($value)
  25. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel whereRoomId($value)
  26. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel whereUid($value)
  27. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomMemberModel whereUpdatedAt($value)
  28. * @mixin \Eloquent
  29. */
  30. class RoomMemberModel extends Model
  31. {
  32. //
  33. protected $table = 'kdgx_goodnight_room_members';
  34. protected $dateFormat = "U";
  35. protected $fillable = ['uid', 'room_id', 'day1_voice', 'day2_voice', 'day3_voice'];
  36. public function user()
  37. {
  38. return $this->hasOne(UserModel::class, 'uid', 'uid')->select('nickname', 'headimgurl', 'sex');
  39. }
  40. /**
  41. * 获取两个人的共同房间
  42. * @param int $uid
  43. * @param int $friend_uid
  44. * @return array
  45. */
  46. public function friendRooms($uid, $friend_uid)
  47. {
  48. $rooms = \DB::select("SELECT room_id
  49. FROM(
  50. SELECT `room_id`
  51. FROM `kdgx_goodnight_room_members`
  52. WHERE `uid`= ?
  53. UNION ALL
  54. SELECT `room_id`
  55. FROM `kdgx_goodnight_room_members`
  56. WHERE `uid`= ?) as t
  57. GROUP BY room_id
  58. HAVING COUNT(*)>= 2", [$uid, $friend_uid]);
  59. $array = [];
  60. $rooms = array_map(function ($value) use (&$array) {
  61. array_push($array, $value->room_id);
  62. }, $rooms);
  63. return $array;
  64. }
  65. }