Laravel Blade Templating @foreach order

Solution:1

Assuming that your $specialist variable is an Eloquent collection, you can do:

@foreach ($specialist->sortBy('description') as $oneSpecialist)
  <option value="{{ $oneSpecialist->specialist_id }}"> {{ $oneSpecialist->description }}</option>
@endforeach

Moreover, you could call your Eloquent model directly from the template:

@foreach (Specialist::all()->sortBy('description') as $oneSpecialist)
  <option value="{{ $oneSpecialist->specialist_id }}"> {{ $oneSpecialist->description }}</option>
@endforeach

Note that you are using a misleading variable name of $key in your foreach() loop. Your $key is actually an array item, not a key. I assume that you saw foreach ($array as $key => $value) syntax somewhere and then removed the $value?

Solution:2

I would suggest using a laravel collection. More specifically, the sortBy(). You can use either of these in your view or the controller that you are passing the data from. If the data is being passed by a model, be sure to use the collect() function before proceeding to use any of the others listed.

Hope this helps!