
Retain selected value of select box in Laravel
Retain selected value of select box in Laravel
In this session, we’ll try our hand at solving the "Retain selected value of select box in Laravel" puzzle by using the computer language.
How can I retain the selected value of the select option while editing the form data in case of Laravel validation failure. I want to the selected option to be selected if validation fails while submitting a form.-
Retain selected value of select box while editing the form in Laravel
//Controller's method code (in controller) $user = User::findOrFail($id); $companies = Company::all();
//Blade file (in blade) <select class="form-control" name="company"> @foreach($companies as $company) <option value="{{ $company->id }}" @if(old('company') == $company->id || $company->id == $user->company_id) selected @endif>{{ $company->name }}</option> @endforeach </select>
You can display the selected option of select box by using the old helper function.
Here we have two models User and Company. User model is used to store the user details in users table with
company id in company_id column which is a foreign key belongs to companies table.
Company model is used to store company name with company id which is primary key of the companies table. -
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>
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Laravel recursive function in controller
- How to display order by null last in laravel
- Laravel API response format
- Route not defined in Laravel
- How to get last month records in Laravel
- Laravel migration add foreign key to existing table
- How to customize or Change validation error messages
- How to customize pagination view in laravel
- How to display user profile after login in laravel
- Display message with session flash using bootstrap alert class in laravel
- Cast Array to an Object in Controller and then pass to view in laravel
- Conditional where clause in Laravel
- How to get query string value in laravel
- How to pass variable from controller to model in Laravel
- Composer\Exception\NoSslException
- Check if Relationship Method Exists in Laravel
- How to use or operator in laravel
- Update email with unique validation in laravel
- Skip first n record and display rest records in laravel view
- Get content from web URL in laravel
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Shorter syntax for whereHas with call back function in laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- On delete set foreign id column value null using migration in laravel 8