movie-night-api/app/Models/MovieList.php

45 lines
939 B
PHP
Raw Normal View History

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-05 00:37:44 -05:00
public function getUserRole($userId)
{
return $this->collaborators()
->where('user_id', $userId)
->first()
?->pivot
->role;
}
public function collaborators(): BelongsToMany
{
2026-04-05 00:37:44 -05:00
return $this->belongsToMany(User::class, 'movie_list_user')
->withPivot('role');
}
2026-02-18 00:15:02 -06:00
}