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
- Docker important commands to run laravel application with docker
- Remove array keys and values if it does not exist in other array in Laravel
- Eager loading dynamically in laravel
- How to pass external link in laravel blade to anchor tag
- How to display validation error in laravel
- Laravel 9 pagination with search filter
- Get 30 days older records from table in laravel
- Setup laravel project with docker
- How to insert ckeditor data into database in Laravel?
- Extra Filter Query on Relationships in Laravel
- OrderBy on Eloquent relationships method in Laravel
- How to create laravel project using jetstream
- 419 page expired error in Laravel
- Remove several global scope from query
- Pass variable from blade to controller Laravel
- Get last week data in Laravel
- Create project table with model and migration
- The use statement with non-compound name 'Auth' has no effect
- How to pass data to route in laravel?
- How to create pivot table in laravel using migration
- Composer\Exception\NoSslException
- Laravel form request validation
- How to create controller in laravel
- Laravel delete all rows older than 30 days
- Json encode method in laravel