SystemConfig.php 1.41 KB
<?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');
    }
}