Laravel Blade Templating @foreach order

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Sorting Laravel Blade @foreach Loops Body: Is there any way to sort @foreach loop in laravel blade? It's natural for developers to work with large amounts of data, and when displaying them using a foreach loop, it is crucial that the results are ordered as desired. In this article, we will discuss how to properly order Laravel Blade @foreach loops. Let's first examine the following code snippet:
@foreach ($specialist as $key)
  <option value="{{$key->specialist_id}}"> {{$key->description}}</option>
@endforeach
The loop iterates through the collection of specialists, printing each element's specialist_id and description. We want to order these results based on their descriptions. Although you can sort the data in your controller by using something like:
->orderBy('description')
There might be a situation where the controller returns other values that also need to be sorted, so we will learn how to achieve order within Laravel Blade. There are two possible approaches you can take for this: 1. Sorting in JavaScript using native functions like Array.prototype.sort() or by creating a custom compare function. 2. Piping the collection through a Blade component with sorting logic built-in. For the first approach, you could modify your code as follows:
@foreach ($specialist->sort(fn($a,$b) => $a['description'] <=> $b['description']) as $key)
  <option value="{{$key->specialist_id}}"> {{$key->description}}</option>
@endforeach
In this example, we use the built-in sort() method of PHP to create a new collection ordered by descriptions. The compare function is defined using an arrow function that compares two elements based on their 'description' key. For the second approach, you could create a new Blade component called SortableComponent with a render() method, as shown below:
@php
namespace App\Components;

use Illuminate\View\Component;

class SortableComponent extends Component {
  public $items;

  /**
   * Create a new SortableComponent instance.
   *
   * @param array $items The items to sort
   */
  public function __construct(array $items)
  {
    $this->items = $items;
  }

  public function render()
  {
    return view('components.sortable')
      ->with([
        'items' => $this->sortedItems()
      ]);
  }

  /**
   * Sorts the items by a specific key and value.
   */
  protected function sortedItems(): array
  {
    return $this->items->sort(fn($a, $b) => $a['description'] <=> $b['description']);
  }
}
@foreach ($specialist as $key)
@include('components.sortable', [
  'items' => $specialist
])
@endforeach
In this case, the @foreach statements render a new instance of SortableComponent, containing pre-ordered data items. This method provides additional flexibility and modularity to your code. To conclude, there are multiple ways to sort Laravel Blade @foreach loops depending on your requirements. You can choose between techniques like sorting in JavaScript or creating a custom component with built-in sorting capabilities. Remember that it's crucial to balance performance and maintainability while handling large amounts of data in your applications.