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 upload file with original file name
- Laravel route redirect not working
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- How to remove P tag from CkEditor in Laravel?
- Add a subselect based on relationship using withAggregate method
- Laravel csrf token mismatch for ajax POST Request
- Display success message in laravel
- If condition in foreach loop in laravel
- How to get all posts which contains comments in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Get all users except the followings users in overtrue laravel-follow
- How to check record exist or not in relationship table
- Return view from route Laravel
- Laravel 9 route group with controller
- Get id of last inserted record in laravel
- How to use or operator in laravel
- Seed database using SQL file in Laravel
- Use of undefined constant laravel
- Get latest record by created at in Laravel
- Call to a member function update() on null
- Method Illuminate\Events\Dispatcher::fire does not exist
- Remove several global scope from query
- How to get records in random order in laravel
- Laravel file size validation not working
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied