laravelmodelrepository

Using different states of a function in a model in different APIs


I am using laravel 8. I have two different function in repository that use same model. I have a function in model that should be different for each function from repository. how can I say into model different condition? I used attributes and append items but it did not work correctly. Is there a way to handle that?

In Repository:

public function paginate(array $options = [])
    {
        $typeCode = $options['typeCode'] ?? null;

        $user = Auth::user();
        $is_admin = $user->isAdmin();

        if(!$is_admin && $typeCode === null) {
            throw BaseException::withMessages([
                __('Gis::error.map_layer.not_allowed')
            ]);
        }

        $query = $this->model
            ->whereNull('ParentId')
            ->with(['descendants'])
            ->orderBy('Id', 'asc');
        if ($typeCode !== null) {
            $query->whereHas('MapLayerGroupType', function ($query) use ($typeCode) {
                $query->where('Code', $typeCode);
            });
        }

        $this->setQuery($query);

        return parent::paginate($options);
    }
    
public function mapGroupLayersList()
    {
        $query = $this->model
            ->with(['descendants'])
            ->whereNull('ParentId')
            ->orderBy('Id', 'asc');

        $this->setQuery($query);

        $result = parent::paginate();

        return $result;
    }

In Model:

    public $timestamps = false;
    protected $guarded = ['Id'];
    protected $table = 'MapLayerGroup';

    protected $appends = ['leaf', 'children'];
    protected $hidden = ['descendants', 'mapLayers'];


    public function parent()
    {
        return $this->belongsTo(MapLayerGroupModel::class, 'ParentId');
    }

    public function descendants()
    {
        return $this->hasMany(MapLayerGroupModel::class, 'ParentId')->orderBy('id', 'asc');
    }

    public function mapLayers()
    {
        return $this->hasMany(MapLayerModel::class, 'MapLayerGroupId', 'Id');
    }

    public function getChildrenAttribute()
    {

        if ('lablabla..' = true) {
            return $this->descendants->toArray();
        }
        else {
            return array_merge(
                $this->descendants->toArray(),
                $this->mapLayers->toArray()
            );

        }

    }

    public function getLeafAttribute()
    {
        return count($this->children) === 0;  // If no children, it's a leaf (true), otherwise false
       
    }
    

Solution

  • I used Static Property to handle functions model.

    In model:

    public static $StaticProperty = false;
    public function getChildrenAttribute()
    {
        if (self::$StaticProperty) {
            return $this->descendants->toArray();
        }
    
        return array_merge(
            $this->descendants->toArray(),
            $this->mapLayers->toArray()
        );
    }
    

    In Repository:

    public function paginate(array $options = [])
    {
        MapLayerGroupModel::$StaticProperty = false;
        // Rest Codes
    }
    
    public function personalMapLayerGroupList()
    {
        MapLayerGroupModel::$StaticProperty = true;
        // Rest Codes
    }