include_all_form_fields parameter for subfields in laravel backpack

39 views Asked by At

I want to complicate the question a little from here. If we have one form, then we can easily process the request in a fetch function. How can we implement the search if we have several identical forms in the subfields? For example

public function setupCreateOperation()
{
    ...
    $this->crud->addFields([
        [
        'name'  => 'anyRows',
        'type'  => 'relationship',
        'label' => 'Table 1-N',
        'subfields'   => [
            [
                'name'    => 'status',
                'label'   => trans($this->trans_prefix . 'status'),
                'type'    => 'select_from_array',
                'options' => [
                    0 => 'one',
                    1 => 'two'
                ],
            ],
            [
                'name'                    => 'service_id',
                'label'                   => trans($this->trans_prefix . 'service'),
                'type'                    => 'select2_from_ajax',
                'entity'                  => 'service',
                'method'                  => 'post',
                'data_source'             => url('fetch/service'),
                'minimum_input_length'    => 0,
                'include_all_form_fields' => true
            ]
        ]
    ]);
}

What should we do in this case? So that a fetch request for a service field would depend only on the corresponding status field? It seems to me that you need to somehow pass the row number in the anyRows array in order to parse the request in the future. Is it possible to do this?

public function fetchService($args)
{
    dd($args); // args - number in anyRows array
}

Data request now:

array:2 [ 
    "q" => ""
    "form" => array:4 [
        0 => array:2 [
          "name" => "anyRows[0][status]"
          "value" => "1"
        ]
        1 => array:2 [
          "name" => "anyRows[0][service]"
          "value" => null <- The collection to select depends on the parameter anyRows[0][status]
        ]
        2 => array:2 [
          "name" => "anyRows[1][status]"
          "value" => "0"
        ]
        3 => array:2 [
          "name" => "anyRows[1][service]"
          "value" => null <- The collection to select depends on the parameter anyRows[1][status]
        ]
    ]
]
1

There are 1 answers

1
Karan Datwani On

I tested something similar and got the subfield's values in the Ajax request: enter image description here

I recommend you create a custom API route instead of fetchOperation for your case flexibility. You can find an example here in the demo repo. link