Code Monkey home page Code Monkey logo

Comments (2)

Norgul avatar Norgul commented on July 22, 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

from nova-multiselect-field.

KasparRosin avatar KasparRosin commented on July 22, 2024

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.

from nova-multiselect-field.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.