[Bug] select2 field that depends on another field not working after 4.1 upgrade
See original GitHub issueBug report
After upgrading to 4.1 the select2 from ajax fields depending on other fields stop working, do some debug and notice that when making the ajax call the request is arriving empty to my controller, so i cant do the filter. Im using the old method that calls to the custom controller to respond to the ajax call. The debuger shows the error when trying to look in $form[‘menu_id’] (Undefined index: menu_id) The index function to manage the ajax call is
public function index(Request $request)
{
$search_term = $request->input('q');
$form = collect($request->input('form'))->pluck('value', 'name');
$options = collect();
// if no category has been selected, show no options
if (!$form['menu_id']) {
return [];
}
// if a category has been selected, only show articles in that category
if ($form['menu_id']) {
$options = $this->platosDisponibles($request);
}
if ($search_term) {
$options=$options->filter(function ($item) use ($search_term) {
// replace stristr with your choice of matching function
return false !== stristr($item->descripcion, $search_term);
})->paginate(10);
} else {
$options = $options->paginate(10);
}
return $options;
}
My field definition
$this->crud->addField(
[ // Select2
'label' => "Plato",
'type' => "select2_from_ajax",
'name' => 'plato_id', // the db column for the foreign key
'entity' => 'plato', // the method that defines the relationship in your Model
'attribute' => "descripcion", // foreign key attribute that is shown to user
'model' => "App\Models\Plato", // foreign key model
'data_source' => url("admin/calculoPreparacionPlatos"), // url to controller search function (with /{id} should return model)
'placeholder' => 'Seleccione un plato', // placeholder for the select
'minimum_input_length' => 0, // minimum characters to type before querying results
'dependencies' => ['fecha', 'menu_id'],
//'method' => 'get',
]
);
My route inside custom.php
Route::group([
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
'namespace' => 'App\Http\Controllers\Admin',
], function () {
Route::get('/calculoPreparacionPlatos', 'Extra\CalculoPreparacionPlatos@index');
}
PHP 7.2.11 (cli) (built: Oct 10 2018 02:04:07) ( ZTS MSVC15 (Visual C++ 2017) x64 ) Copyright © 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright © 1998-2018 Zend Technologies with Xdebug v2.7.2, Copyright © 2002-2019, by Derick Rethans
LARAVEL VERSION:
v7.12.0@c2fff1e9879494a6f853593b3c517dc9922bbb51
BACKPACK VERSION:
4.1.6@cbd4143d3eb8302916012af205565cd3183f274f
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
Hmm… that’s… wow… yeah… that sounds way better, doesn’t it @pxpm 😆 ? If you have
dependencies
, you MUST need them in the AJAX call.I’ve opened #3066 for this, made a summary so it’s easier to understand what we’re all taking about, instead of having to read a bunch of threads. Let’s move the conversation there, should be easier to follow.
Indeed @tabacitu . That was introduced here: https://github.com/Laravel-Backpack/CRUD/pull/2757/commits/2a0d1215a2d9c2a34882a644a01b2f45654f3159
I’v just submited the PR to the docs.
Best, Pedro