40 lines
1.0 KiB
PHP
40 lines
1.0 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||
|
|
|
||
|
|
#[Fillable(['name', 'description', 'owner_id'])]
|
||
|
|
class Group extends Model
|
||
|
|
{
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
public function owner(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(User::class, 'owner_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function members(): BelongsToMany
|
||
|
|
{
|
||
|
|
return $this->belongsToMany(User::class)->withTimestamps();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function investigations(): BelongsToMany
|
||
|
|
{
|
||
|
|
return $this->belongsToMany(Investigation::class, 'investigation_group')
|
||
|
|
->withPivot('role')
|
||
|
|
->withTimestamps();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function isAccessibleBy(User $user): bool
|
||
|
|
{
|
||
|
|
return $this->owner_id === $user->id
|
||
|
|
|| $this->members()->where('users.id', $user->id)->exists();
|
||
|
|
}
|
||
|
|
}
|