1

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.

Jaaayz
  • 1,533
  • 7
  • 27
  • 59
  • When the user selects a different service, the options in the `categories_id` select should change? You'll need JavaScript code for that. – Jonas Staudenmeir Aug 04 '18 at 15:27

1 Answers1

0

I would take a look at the values you are passing to pluck. Since you are generating a new collection with the second query, I believe you should be able to call pluck('name', 'id');. The categories.column_name is used to avoid ambiguity in the query, but all the collection is doing is retrieving models, so if you use the property names without the table prefix - you should get what you're after.

I previously assumed that you were handling the sorting via query; since you are handling that at the collection level as well, I'd remove the categories prefix from that field too. You could also adjust the query to use the ->orderBy() method and pass the field names as an array to get (like so: ->get(['name', 'id'])). It all depends on your needs - if my first suggestion doesn't work for you, you could move the sorting and filtering to the query. Hope that helps some.

frosty
  • 769
  • 6
  • 18
  • So I would use the `pluck` method without the `categories` prefix before the column name right? I just refered to this [question] (https://stackoverflow.com/questions/40635146/laravel-pluck-fields-from-relations) but didn't help me out. – Jaaayz Aug 03 '18 at 13:40
  • @jaaayz I would personally do it at the query level - something like: `$categories = Service::with('categories')->orderBy('categories.name', 'asc')->get(['categories.name', 'categories.id']);` – frosty Aug 03 '18 at 13:45
  • But I would change my dropdown into foreach right? Correct me if I'm wrong. That query would't work with the blade template dropdown – Jaaayz Aug 03 '18 at 13:49
  • I have an alternative solution that worked for me. But I think this is isn't quite right take a loot at my query this works. ` $categories = Categories::with('service')->whereHas('service', function($query) use ($service) { $query->where('id', $service); }) ->get()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('name', 'id');` – Jaaayz Aug 03 '18 at 14:00
  • Looks like your where method needs an operator `where('id', '=', $service)` and if `$service` is an object you'll probably have to specify the property (`id, category_id, or whatever`) - hard to know without seeing it, but that kinda jumped out at me. – frosty Aug 03 '18 at 16:48