2026-02-18 00:15:02 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-04-05 00:37:44 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-02-18 00:15:02 -06:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
|
|
|
|
|
|
class MovieList extends Model
|
|
|
|
|
{
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
|
|
|
|
'is_public',
|
|
|
|
|
'owner',
|
|
|
|
|
'slug',
|
|
|
|
|
];
|
|
|
|
|
|
2026-04-05 00:37:44 -05:00
|
|
|
protected $with = ['listOwner'];
|
|
|
|
|
|
|
|
|
|
public function listOwner(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'owner');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 00:15:02 -06:00
|
|
|
public function movies(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Movie::class);
|
|
|
|
|
}
|
2026-04-03 00:39:37 -05:00
|
|
|
|
2026-04-09 18:30:16 -05:00
|
|
|
public function getUserRole($userId): string
|
2026-04-05 00:37:44 -05:00
|
|
|
{
|
2026-04-09 18:30:16 -05:00
|
|
|
$roleId = $this->collaborators()
|
2026-04-05 00:37:44 -05:00
|
|
|
->where('user_id', $userId)
|
|
|
|
|
->first()
|
2026-04-09 18:30:16 -05:00
|
|
|
?->pivot->role_id;
|
|
|
|
|
|
|
|
|
|
return Role::query()->find($roleId)?->name;
|
2026-04-05 00:37:44 -05:00
|
|
|
}
|
|
|
|
|
|
2026-04-03 00:39:37 -05:00
|
|
|
public function collaborators(): BelongsToMany
|
|
|
|
|
{
|
2026-04-05 00:37:44 -05:00
|
|
|
return $this->belongsToMany(User::class, 'movie_list_user')
|
2026-04-09 18:30:16 -05:00
|
|
|
->withPivot('role_id');
|
2026-04-03 00:39:37 -05:00
|
|
|
}
|
2026-02-18 00:15:02 -06:00
|
|
|
}
|