Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async model querying with filter with api() #250

Open
mattiaglacom opened this issue Jan 31, 2024 · 2 comments
Open

Async model querying with filter with api() #250

mattiaglacom opened this issue Jan 31, 2024 · 2 comments
Labels
tutorial Nothing's wrong, just a helpful tip on how to do something.

Comments

@mattiaglacom
Copy link

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){ // ?? })

@Norgul
Copy link

Norgul commented Jul 15, 2024

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:

public function __invoke(Request $request): JsonResponse
{
    $search = $request->get('search'); // <--- you will get user input through this

    if (!$search) {
        return response()->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()) {
        return response()->json();
    }

    return response()->json($products->pluck('title', 'id')); // <--- pluck the results for showing them in the frontend correctly
}

Some notes:

  1. If you return response()->json([]), you will get empty row back instead of "No results found"
  2. Plucking needs to have id set as key so you can pick it up later
  3. I found no value in api() third parameter ($keyName), as it didn't change a thing

Hope it helps

@KasparRosin KasparRosin added the tutorial Nothing's wrong, just a helpful tip on how to do something. label Jul 16, 2024
@KasparRosin
Copy link
Member

I'll leave it with tutorial label at the moment.
If anybody has time and interest, we are open to pull requests with a summary to update the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tutorial Nothing's wrong, just a helpful tip on how to do something.
Projects
None yet
Development

No branches or pull requests

3 participants