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
- Route group with URI prefix using middleware and route name prefixes
- The use statement with non-compound name 'Auth' has no effect
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- How to display user profile after login in laravel
- How to display pivot table column value in laravel
- Get only 10 records from table in laravel
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Method Illuminate\Events\Dispatcher::fire does not exist
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- Get comma separated email from input array
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- Laravel onclick function not working
- If no route matched route::fallback in laravel
- Ignore Records where a field has NULL value in Laravel
- How to get IP address in laravel
- How to show data by ID in laravel?
- Print query in laravel
- Array to string conversion laravel blade
- How to get column names from table in Laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Redirect from www to non www in laravel using htaccess
- Return redirect laravel not working
- How to start websocket server in laravel
- How to upload image in laravel 8
- Split an Eloquent Collection by half in Laravel