Display option of select as selected with blade directive Laravel

Updated at 22-Aug-2022, By samar

Display option of select as selected with blade directive Laravel

With this article, we will examine several different instances of how to solve the "Display option of select as selected with blade directive Laravel".

Here we have a blade directive `@selected` in Laravel 9 to display the option of select box as selected with condition. Like if we want to display the old value to be selected of the select box which we have selected while submit the form data then `@selected` directive is very helpful to do so.

Syntax: @selected(old('version') == $version)

  • <select name="version">
        @foreach ($product->versions as $version)
            <option value="{{ $version }}" @selected(old('version') == $version)>
                {{ $version }}
            </option>
        @endforeach
    </select>
    
  • Select option to be selected while creating a record in Laravel using old helper method

    <option value="{{ $company->id }}" @if(old('company') == $company->id) selected @endif>
    	{{ $company->name }}
    </option>
    

Back to code snippet queries related laravel