In my laravel application I have a simple form with a dropdown.
Dropdown options are getting filled with DB data.
I want to populate my dropdown with user previously selected option on the edit.
Following is what I have done so far..
Blade
<select name="company_id" class="form-control">
#foreach($companies as $company)
#if (old('company_id') == $employee->company_id)
<option value="{{ $company->id }}" selected>{{ $company->name }}</option>
#else
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endif
#endforeach
</select>
Controller.
public function edit(Employee $employee)
{
$companies = Company::all(['id','name']);
return view('employees.edit', compact('employee','companies'));
}
Employee Model
{
use HasFactory, Notifiable;
protected $fillable = [
'first_name', 'last_name', 'email', 'phone', 'company_id'
];
public function company()
{
return $this->belongsTo(Company::class);
}
}
My company table structure
Employee table structure
When I tried with these, it kept showing me the values in the dropdown on the edit but not setting the old value properly.....
Try this as your loop:
#foreach ($companies as $company)
<option value="{{ $company->id }}" {{ $company->id == old('company_id') ? 'selected' : '' }}>
{{ $company->name_en }}</option>
#endforeach
The reason could be typecasting.
#if (old('company_id') === $employee->company_id)
This will never be true. You see old holds string values and you're comparing it against an id which is of type int.
So, this will work for you:
#if ( (int)old('company_id') === $employee->company_id )
Also, there is a new directive you can use instead of conventional if-else statements checked :
#checked( old('key', $employee->key) )
#selected( old('key') === $employee->key )
In Laravel 9 you don't have to use if-else to check the value just use the #selected() blade function/directive.
<select name="company_id" class="form-control">
#foreach($companies as $company)
<option value="{{ $company->id }}" #selected(old('company_id') == $company->id)>{{ $company->name }}</option>
#endforeach
</select>
Also, we have the #checked() function as well:
<input type="checkbox" name="active" value="active" #checked(old('active', $user->active)) />
Related
I have three tables: products, categories and category_product.
In my edit form page I'm displaying the categories and subcategories values in a select box.
The chosen category options are saved in the pivot table category_product. This table has product_id and category_id.
Here's my code.
Product model
public function category() {
return $this->belongsToMany('App\Models\Category');
}
Category model:
public function products()
{
return $this->belongsToMany(Product::class);
}
edit product page view:
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}">{{ $parentCategory->name }}</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>
subcats-select view
#foreach($subcategories as $subcategory)
<option value="{{ $subcategory->id }}">---{{ $subcategory->name }}</option>
#if(count($subcategory->subcategory))
#include('includes.subcats-select',['subcategories' => $subcategory->subcategory])
#endif
</div>
#endforeach
How can I put "selected" attribute to the chosen category options, so when I edit the page to apply the changes properly?
You can get pivot table columns the following way:
Product model
public function category() {
return $this->belongsToMany('App\Models\Category')->withPivot('selected');
}
edit product page View:
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}" selected="{{ $parentCategory->pivot->selected }}">
{{ $parentCategory->name }}
</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>
EDIT: While the above answer might be helpful for others, it doesn't answer the question. So here's my second try after some clarification in the chat.
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}"{{ $product->category->contains($parentCategory->id) ? 'selected' : '' }}>
{{ $parentCategory->name }}
</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>
i want to edit user profile which has a country, country field is a select option, i want to get a user's country value in select field, i tried the answer in stack but it didn't work for me knowing there is relationship between user and country belongsTo hasMany.
edit.blade.php
<select id="country" name="country_id" class="form-control" >
<option value="" selected disabled>Select</option>
#foreach($countries as $country)
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
#endforeach
</select>
usersController.php
public function edit($id)
{
$users = Auth::user();
$countries = Country::all();
return view('users.edit')->with([
'users' => $users,
'countries' => $countries
]);
}
Instead of this:
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
I believe you want this:
<option value="{{$country->id}}"{{ $country->id == $users->country_id ? ' selected' : '' }}> {{ $country->name }}</option>
Can someone tell me how can I get selected value when I try to edit post in Laravel project.
So here are parts of code
Post.php:
public function category(){
return $this->belongsTo('App\Category');
}
Category.php:
public function posts(){
return $this->hasMany('App\Post');
}
PostController.php:
$post = Post::findOrFail($id);
$categories = Category::all();
$tags = Tag::all();
return view('admin.posts.edit', ['post' => $post, 'categories' => $categories, 'tags' => $tags]);
posts/edit.blade.php:
<div class="form-group">
<label for="category_id">Selecet category</label>
<select name="category_id" id="category_id" class="form-control">
<option disabled>List of available post categories</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
Like #Hardood mentioned. $category->id == $category->id will always return true. In fact, if you just want to get the value from form submission. You can just remove that. HTML will handle selected value itself.
You can simply use $request->category_name in your backend.
Problem solved with this part of code (for future users who will read this :) )
#foreach($categories as $category)
<option value="{{ $category->id }}"
#if($post->category->id == $category->id)
selected
#endif
>{{ $category->name }}</option>
#endforeach
I create a fuction for checking selected option and I want to call it in the
for the selected part.
#foreach ($stocks as $stock)
<option value="{{ $stock->id }}" ..... > {{ $stock->name }}</option>
#endforeach
for the .... should be the place to call the function that I created but I don't know how to call it.
I tried :
#foreach ($stocks as $stock)
<option value="{{ $stock->id }}" {{ CheckCombo($stock->id, $product-
product_type }} > {{ $stock->name }}</option>
#endforeach
for the function CheckCombo, it returns 'selected' back.
When you loop the option values check it with the db value and if it gets a match echo seleceted
#foreach ($stocks as $stock)
<option value="{{ $stock->id }}" #if( ($stock->id) == ($product->
product_type)) selected #endif > {{ $stock->name }}</option>
#endforeach
I want to fetch drop down option value & name from database, to do that my controller code is
$roles = Role::pluck('role','user_id');
return view('users.add_role',compact('roles'));
to fetch this data from drop down list my view file code is
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role->user_id }} "> {{ $role->role }} </option>
#endforeach
</select>
but it says error
Trying to get property of non-object (View: C:\xampp\htdocs\auth2\resources\views\users\add_role.blade.php)
if i just only use
<select name="role" class="form-control" >
#foreach($roles as $role)
<option value="{{ $role }} "> {{ $role }} </option>
#endforeach
</select>
then it shows role column of the table, but it not pass option value to database. so what is the correct way to generate option value & name from database?
Your $roles variable containt an array which is looks like
[0] => 'your_role'
[1] => 'your_role'
Where is 0 and 1 index is user_id. So your user_id is set as an index of your $roles array. Simply do it dd($roles) and check the output. $key as $role will work. Where $key will containt the user_id.
#foreach($roles as $key => $role)
<option value="{{ $key }} "> {{ $role }} </option>
#endforeach
I use laravel collective package to generate dropdown list much more easier,
you can do things like, on your blade template and it will render the list for you
{{ Form::select('role', $role )) }}