Search code examples
phplaraveloctobercmsoctobercms-backend

OctoberCms record finder with dynamic scope


I'm using OctoberCMS based on Laravel and trying to get a list of products within a form via Record Finder.

The use-case is that the record-finder must show available products based on dynamic condition.

I tried to achieve this via "Scope" option of record finder for related form model but not finding a way to pass the dynamic value to the scope.

Sample Code --

class A extends Model
{
    public $belongsTo = [
        'product' => [
            'Plugin\Models\B', 
            'key' => 'id',
            'scope' => 'specificProduct'
        ],   
    ];
}

class B extends Model
{
    public function scopeSpecificProduct($query , $product_type)
    {
        return $query->where('product_type', $product_type);
    }
}

Here $product_type is the dynamic value which I am trying to pass via record finder and get in scope.

Can anyone suggest that is this a correct way for such requirement or how should I achieve this ?


Solution

  • In your fields definition you have to use the scope attribute

    fields:
        products:
            label: Products
            type: recordfinder
            scope: specificProduct
    

    With this, the second param of your scope will be the A model that is creating or updating

    class B extends Model
    {
        public function scopeSpecificProduct($query , $model)
        {
            return $query->where('product_type', $model->depend_attribute);
        }
    }