moved roles to a separate database and added code to make updates
This commit is contained in:
parent
985f339725
commit
836ef8f1f6
14 changed files with 317 additions and 21 deletions
|
|
@ -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
11
app/Models/Role.php
Normal 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;
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue