moved roles to a separate database and added code to make updates

This commit is contained in:
Edward Tirado Jr 2026-04-09 18:30:16 -05:00
parent 985f339725
commit 836ef8f1f6
14 changed files with 317 additions and 21 deletions

View file

@ -27,18 +27,19 @@ class MovieList extends Model
return $this->belongsToMany(Movie::class);
}
public function getUserRole($userId)
public function getUserRole($userId): string
{
return $this->collaborators()
$roleId = $this->collaborators()
->where('user_id', $userId)
->first()
?->pivot
->role;
?->pivot->role_id;
return Role::query()->find($roleId)?->name;
}
public function collaborators(): BelongsToMany
{
return $this->belongsToMany(User::class, 'movie_list_user')
->withPivot('role');
->withPivot('role_id');
}
}

11
app/Models/Role.php Normal file
View file

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
use HasFactory;
}

View file

@ -45,9 +45,19 @@ class User extends Authenticatable
return $this->hasMany(MovieList::class, 'owner');
}
public function hasRole(MovieList $movieList, int $role): bool
{
return $this->sharedLists()
->wherePivot('movie_list_id', $movieList->id)
->wherePivot('role_id', $role)
->exists();
}
public function sharedLists(): BelongsToMany
{
return $this->belongsToMany(MovieList::class)->withPivot('role')->withTimestamps();
return $this->belongsToMany(MovieList::class)
->withPivot('role_id')
->withTimestamps();
}
/**