How do I use this same structure in Backpack 6.x? I have a table of States and another of Cities and I need the city combo to depend on the state information.
On Controller:
[
'name' => 'state_id',
'label' => 'Estado',
'type' => 'select2',
'entity' => 'state', // the method that defines the relationship in your Model
'model' => "App\Models\State", // foreign key model
'attribute' => 'uf_name', // foreign key attribute that is shown to user
'allows_null' => true,
'placeholder' => "Selecione o Estado",
'options' => (function ($query) {
return $query->orderBy('uf', 'ASC')->get();
}),
],
[ // select2_from_ajax: 1-n relationship
'name' => 'city_id',
'label' => 'Município', // Table column heading
'type' => 'select2_from_ajax',
'ajax' => false,
'model' => "App\Models\City", // foreign key model
'entity' => 'city', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'data_source' => url('api/city'), // url to controller search function (with /{id} should return model)
'method' => 'GET', // route method, either GET or POST
'allows_null' => true,
'placeholder' => 'Selecione o município', // placeholder for the select
'minimum_input_length' => 0, // minimum characters to type before querying results
'dependencies' => ['state_id'],
],
On City Api:
public function index(Request $request)
{
$search_term = $request->input('q');
$form = collect($request->input('form'))->pluck('value', 'name');
$options = City::query();
// if no category has been selected, show no options
if (!$form['state_id']) {
return [];
}
// if a category has been selected, only show articles in that category
if ($form['state_id']) {
$options = $options->where('state_id', $form['state_id']);
}
if ($search_term) {
return $options->where('name', 'ilike', '%' . strtoupper($search_term) . '%')
->orderBy('name')
->paginate(10);
}
return $options->paginate(10);
}
public function show($id)
{
return City::find($id);
}
Return Error: Undefined array key "state_id"
Could you help me with this problem or give me a cool alternative?
How do I use this same structure in Backpack 6.x? I have a table of States and another of Cities and I need the city combo to depend on the state information.
On Controller:
On City Api:
Return Error: Undefined array key "state_id"
Could you help me with this problem or give me a cool alternative?
Originally posted by @helesjunior in https://github.com/Laravel-Backpack/CRUD/issues/1484#issuecomment-2148305097