SystemConfig.php
1.41 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace App\Models;
use App\Support\Model;
use Arr;
use Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo;
use Awobaz\Compoships\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;
/**
* @method static Builder parentKey(array|string $value)
*/
class SystemConfig extends Model
{
protected $table = 'system_config';
protected $guarded = [];
protected $hidden = ['deleted_at'];
/**
* @inheritDoc
*/
protected static function booted(): void
{
static::addGlobalScope('show', static fn(Builder $builder) => $builder->where('is_show', 1));
}
protected $casts = [
'expand' => 'json'
];
/**
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param array|string $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeParentKey(Builder $builder, array|string $value): Builder
{
return $builder->whereHas('parent', fn(Builder $builder) => $builder->where('status', 1)->whereIn('identifier', Arr::wrap($value)));
}
/**
* @return BelongsTo
*/
public function parent(): BelongsTo
{
return $this->belongsTo(__CLASS__);
}
/**
* @return HasMany
*/
public function children(): HasMany
{
return $this->hasMany(__CLASS__, 'parent_id')->latest('weight')->with('children');
}
}