1 | <?php |
||
2 | |||
3 | namespace App\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | |||
7 | /** |
||
8 | * App\Models\UserMovie. |
||
9 | * |
||
10 | * @property int $id |
||
11 | * @property int $users_id |
||
12 | * @property int|null $imdbid |
||
13 | * @property string|null $categories List of categories for user movies |
||
14 | * @property \Carbon\Carbon|null $created_at |
||
15 | * @property \Carbon\Carbon|null $updated_at |
||
16 | * |
||
17 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie whereCategories($value) |
||
18 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie whereCreatedAt($value) |
||
19 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie whereId($value) |
||
20 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie whereImdbid($value) |
||
21 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie whereUpdatedAt($value) |
||
22 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie whereUsersId($value) |
||
23 | * |
||
24 | * @mixin \Eloquent |
||
25 | * |
||
26 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie newModelQuery() |
||
27 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie newQuery() |
||
28 | * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserMovie query() |
||
29 | */ |
||
30 | class UserMovie extends Model |
||
31 | { |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $guarded = []; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $dateFormat = false; |
||
41 | |||
42 | /** |
||
43 | * @return int|\Illuminate\Database\Eloquent\Builder |
||
44 | */ |
||
45 | public static function addMovie($uid, $imdbid, array $catID = []) |
||
46 | { |
||
47 | return self::query() |
||
48 | ->insertGetId( |
||
49 | [ |
||
50 | 'users_id' => $uid, |
||
51 | 'imdbid' => $imdbid, |
||
52 | 'categories' => ! empty($catID) ? implode('|', $catID) : 'NULL', |
||
53 | 'created_at' => now(), |
||
54 | ] |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | public static function getMovies($uid): array |
||
59 | { |
||
60 | return self::query() |
||
61 | ->where('users_id', $uid) |
||
62 | ->leftJoin('movieinfo as mi', 'mi.imdbid', '=', 'user_movies.imdbid') |
||
63 | ->orderBy('mi.title') |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
64 | ->get(['user_movies.*', 'mi.year', 'mi.plot', 'mi.cover', 'mi.title']) |
||
65 | ->toArray(); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public static function delMovie($uid, $imdbid) |
||
72 | { |
||
73 | return self::query()->where(['users_id' => $uid, 'imdbid' => $imdbid])->delete(); |
||
74 | } |
||
75 | |||
76 | public static function getMovie($uid, $imdbid): array |
||
77 | { |
||
78 | return self::query() |
||
79 | ->where(['user_movies.users_id' => $uid, 'user_movies.imdbid' => $imdbid]) |
||
80 | ->leftJoin('movieinfo as mi', 'mi.imdbid', '=', 'user_movies.imdbid') |
||
81 | ->get(['user_movies.*', 'mi.title']) |
||
82 | ->toArray(); |
||
83 | } |
||
84 | |||
85 | public static function delMovieForUser($uid) |
||
86 | { |
||
87 | self::query()->where('users_id', $uid)->delete(); |
||
88 | } |
||
89 | |||
90 | public static function updateMovie($uid, $imdbid, array $catID = []) |
||
91 | { |
||
92 | self::query() |
||
93 | ->where(['users_id' => $uid, 'imdbid' => $imdbid]) |
||
94 | ->update(['categories' => ! empty($catID) ? implode('|', $catID) : 'NULL']); |
||
95 | } |
||
96 | } |
||
97 |