generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathHasParentTrait.php
More file actions
40 lines (33 loc) · 908 Bytes
/
HasParentTrait.php
File metadata and controls
40 lines (33 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace Statikbe\FilamentFlexibleContentBlocks\Models\Concerns;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasParent;
/**
* @mixin HasParent
* @mixin Model
*/
trait HasParentTrait
{
public function initializeHasParentTrait(): void
{
$this->mergeFillable(['parent_id']);
}
public function parent(): BelongsTo
{
return $this->belongsTo(static::class, 'parent_id');
}
public function children(): HasMany
{
return $this->hasMany(static::class, 'parent_id');
}
public function hasParent(): bool
{
return ! is_null($this->parent_id);
}
public function isParentOf(HasParent $child): bool
{
return $this->id === $child->parent_id;
}
}