This is a classic, but I’m using the amazing SPLADE from slade.dev, and can’t get it to work. (How is SPLADE not a tag on stackoverflow yet?)
Use case: The user selects a main category, and then only the sub-categories of that category must show in the sub-categories for him to select from.
In the controller:
$categories = Category::whereNull('parent_id')->orderBy('name')->pluck('name', 'id');
In the blade:
<x-splade-select :options="$categories" label="Category" name="category_id" />
<x-splade-select :options="$subcategories" label="Sub-category" name="subcategory_id" remote-url="/getSubcategories/${form.categoryId}" />
getSubcategories is an API that retrieves the subcategories for a given $category_id:
$subcategories = Category::whereNotNull('parent_id')->where('parent_id', $category_id)->pluck('name', 'id');
${form.categoryId} in the blade above is not right. How do I retrieve the current form’s category_id to send to the API?
2
Answers
Solved it:
In blade:
Where $categories is passed from the controller:
and the api returns $subcategories as follows:
I used to deal with this for my project as well.
My solution:
Note: For litle category 🙂