Blade Tempating @foreach Templates
Is there a way to sort the @foreach in a cuff blade?
@foreach ($specialist as $key) <option value="{{$key->specialist_id}}"> {{$key->description}}</option> @endforeach I like to order $key->description ,
I know that I can use order in my controller,
->orderBy('description') but the controller returns other values, and these values ββshould also be sorted, so I need to order by click.
Assuming 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 Alternatively, you can invoke 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 the misleading variable $ key name in your foreach () loop. Your $ key is an array element, not a key. I assume you saw the foreach syntax somewhere ($ array as $ key => $ value) and then deleted the value of $?
I would suggest using laravel collection . In particular, sortBy() . You can use any of them in your view or the controller from which you transfer data. If the data is passed by the model, be sure to use the collect() function before using any of the others listed.
Hope this helps!