I have a Laravel 5.5 app that has an edit or update function that has two dropdowns. One dropdown is the parent and the second dropdown is it child relationship. So basically the first dropdown has many second dropdown thats the case.
Now about the code in my controller
$services = Service::all()->sortBy('code', SORT_NATURAL | SORT_FLAG_CASE)->pluck('description', 'id');
$categories = Service::with('categories')->get()->sortBy('categories.name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('categories.name', 'categories.id');
I used a pluck
method that will retrieve only the name and the id of the categories which is the child relation of service like what I have said. A service has many categories and I only want to display the categories in the dropdown based on the selected service in the dropdown which is automatically chosen base on existing record with the help of Laravel pluck.
However the second dropdown
or the categories
is empty or the second query is not working.
Here is how I defined my relationship in the service model.
public function categories()
{
return $this->hasMany('App\Models\Categories', 'service_id', 'id');
}
Now the code in my blade file
<div class="form-group col-sm-6">
{!! Form::label('service_id', 'Service:') !!}
{!! Form::select('service_id', $services, null, ['class' => 'form-control input-md','required', 'id' => 'service_id'])!!}
</div>
<div class="form-group col-sm-6">
{!! Form::label('categories_id', 'Category:') !!}
{!! Form::select('categories_id', $categories, null, ['class' => 'form-control input-md','required', 'id' => 'category_id'])!!}
</div>
Appreciate if someone could help. Thanks in advance.