
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 pagination links with query string
- Generate unique username in Laravel
- How to check relationship is loaded or not in Laravel
- How to pass variable from controller to model in Laravel
- If no route matched route::fallback in laravel
- Ajax POST request in laravel
- How to start websocket server in laravel
- Convert multidimensional array to single array in Laravel
- Call to undefined method Illuminate\Support\Facades\Request::all()
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Redirect to previous page or url in laravel
- How to create projects method with belongstomany relationship in user model
- How to show data by ID in laravel?
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- How to call Laravel route in jQuery
- OrderBy on Eloquent relationships method in Laravel
- How to remove package from laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- Always load the relationship data with eager loading in Laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- How to get images from AWS s3 and display in Laravel blade
- PhpMyAdmin - Error The mysqli extension is missing
- Get only 10 records from table in laravel
- How to create pivot table in laravel using migration
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied