You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have to load in a multiselect a lot of records (like 50000), so i need async loading, i also need to filter data before display, so i think the solution is api().
But i don't understand how setup the endpoint, how filter data? I have to use eloquent or i have to modify my resource?
Someone have an example of api call and endpoint?
Thanks
my case, i need to filter Magazine Resource with is_active=true ->api('/api/multiselect/magazine?is_active=true', Magazine::class),
Probably answer you might not need anymore, and which should definitely be documented.
You are correct to assume that filtering can be done through api() method. The way to do it is to add it to Multiselect element and publish the route like this:
// In resource
Multiselect::make('Products')
->api('/api/multiselect/products', Product::class)
->singleSelect(), // <-- I am using single select for this, you don't need to.// In api.php
Route::get('multiselect/products', MultiselectProductController::class);
And then this is what the controller should look like:
publicfunction__invoke(Request$request): JsonResponse
{
$search = $request->get('search'); // <--- you will get user input through thisif (!$search) {
returnresponse()->json(); // <--- if you want empty result, you need to return empty json
}
$products = Product::query()
->where('allowed_for_purchase', true)
->where('title', 'LIKE', "%{$search}%")
->get();
if ($products->isEmpty()) {
returnresponse()->json();
}
returnresponse()->json($products->pluck('title', 'id')); // <--- pluck the results for showing them in the frontend correctly
}
Some notes:
If you return response()->json([]), you will get empty row back instead of "No results found"
Plucking needs to have id set as key so you can pick it up later
I found no value in api() third parameter ($keyName), as it didn't change a thing
I have to load in a multiselect a lot of records (like 50000), so i need async loading, i also need to filter data before display, so i think the solution is api().
But i don't understand how setup the endpoint, how filter data? I have to use eloquent or i have to modify my resource?
Someone have an example of api call and endpoint?
Thanks
my case, i need to filter Magazine Resource with is_active=true
->api('/api/multiselect/magazine?is_active=true', Magazine::class),
Route::get('/multiselect/magazine', function(Request $request){ // ?? })
The text was updated successfully, but these errors were encountered: