how to make the old value selected in Laravel 8 blade with select input in edit form.... i tried this way but didn't work:
<select name="category" id="category" class=" #error('category') border-red-500 #enderror rounded-md shadow-sm mt-1 block w-full">
#foreach($categories as $category)
<option value="{{$category->id}}" {{ old('category', $category->id) ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
and this is what inside controller:
public function edit(Product $product)
{
$categories = Category::all();
return view('dashboard.products.edit', compact('categories', 'product'));
}
I think your product should have a category_id so in the loop you need to check it also $product->category_id === $category->id.
#foreach($categories as $category)
<option value="{{$category->id}}" {{ old('category', $category->id) || $product->category_id === $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
Based on the code in edit() method old() wont work. Because there is no old values since your code just return a view.If you need to use that function then you need to redirect with inputs like
return redirect()->back()->withInput();
Related
How can i show in my edit.blade.php the selected category of my product. you can only choose 1 category so is not many to many relation.
Product model :
public function category(){
return $this->belongsTo(Category::class);
}
Category model :
public function products(){
return $this->hasMany(Product::class);
}
Db products:
<div class="form-group my-2">
<label for="category">Choose Category :</label>
<select name="category_id" class="form-control custom-select">
#foreach($categories as $category)
<option value="{{$category->id}}" #if($product->category->contains($category->id)) selected #endif>{{$category->name}}</option>
#endforeach
</select>
</div>
I keep getting errors like :
Call to undefined method App\Models\Category::contains()
Laravel version < 9
<option value="{{ $category->id }}" {{ ($product->category_id == $category->id) ? 'selected' : '' }}>{{ $category->name }}</option>
Larvel version > 9
<option value="{{ $category->id }}" #selected($product->category_id == $category->id)>{{ $category->name }}</option>
Ref: https://laravel.com/docs/9.x/blade#checked-and-selected
Your product belongsTo category:
<option value="{{$category->id}}" #if($product->category_id == $category->id) selected #endif>{{$category->name}}</option>
You could try this
<option value="{{$category->id}}" {{($product->category_id == $category->id) ? 'selected' : ''}}>{{$category->name}}</option>
I want to retrieve database values in category name and i want to show default value in selection. This is my controller for my edit view.
I have a category_product table.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $product->categories()->category_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
ProductController.php
public function edit(Product $product)
{
$categories = Category::all();
return view('Admin.products.edit', compact('product', 'categories'));
}
Product.php
public function categories()
{
return $this->belongsToMany(Category::class);
}
I get this error.
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id
You need to add default value in the select tag above foreach() loop.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
<option value="xyz">xyz</option> /* set default option value */
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
If you want set category name selected then you need to match category in product collection. ( if you have catetory_id in product table )
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$product->catetory_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
I want to show the old select value in this select field when I want to edit. How can I show my old value in these fields?
https://github.com/shakibzaman/book-library-laravel
you can show my GitHub repo-
Here is my rules edit page:
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
<label for="chap_id">{{ trans('cruds.chapters.title_singular') }}*</label>
<select name="chap_id" id="chap_id" class="form-control select2">
#foreach($chapters as $id => $chapter)
<option value="{{ $id }}">{{ $chapter }}</option>
#endforeach
</select>
#if($errors->has('name'))
<em class="invalid-feedback">
{{ $errors->first('name') }}
</em>
#endif
<p class="helper-block">
{{ trans('cruds.permission.fields.title_helper') }}
</p>
</div>
editController
public function edit($id)
{
if (!Gate::allows('users_manage')) {
return abort(401);
}
$rule = Rule::find($id);
$chapters = Chapter::all()->pluck('name', 'id')->prepend(trans('global.pleaseSelect'), '');
return view('admin.rules.edit', compact('rule', 'chapters'));
}
You can use the old() function to get the old value of a field. This function requires one input and that is the name of the field: old('chap_id').
You could use this one:
#foreach($chapters as $id => $chapter)
#if(null !== old('chap_id') && old('chap_id') === $id)
<option value="{{ $id }}" selected>{{ $chapter }}</option>
#else
<option value="{{ $id }}">{{ $chapter }}</option>
#endif
#endforeach
Or the shorter one:
#foreach($chapters as $id => $chapter)
<option value="{{ $id }}" {{ old('chap_id') === $id ? 'selected' : '' }}>{{ $chapter }}</option>
#endforeach
why all categories appear in one post, I just want to display the value category that matches the post.
public function edit(Post $post)
{
$categories = Category::all();
$tags = Tag::all();
return view('admin.post.edit', compact('post', 'categories', 'tags'));
}
<select id="category" name="categories[]" style="width: 100%" multiple>
#foreach($categories as $category)
<option
#foreach ($categories as $postCategory)
{{ $postCategory->id == $category->id ? 'selected' : '' }}
#endforeach
value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
<script>
$(document).ready(function () {
$("#category").select2({
placeholder: "Please Select"
});
});
</script>
Assuming $post will have a category_id you just need to check that field against the id of the current $category being iterated:
<select id="category" name="categories[]" style="width: 100%" multiple>
#foreach($categories as $category)
<option {{ $post->category_id == $category->id ? 'selected' : '' }}
value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
I'm trying to get a old selected value and show it inside a select option, as well as a non selected value, however, when i try to compare then it doesnt seen to work, so far i can only get duplicates. What's the best way to implement it?
That's how my code look like.
Controller:
public function index()
{
$roles = Role::get();
$permissions = Permission::get();
return view('role.index', compact(['roles','permissions']));
}
Role View:
#foreach($role->permission as $permissioninrole)
<option name="permissions[]" {{ old('name', $permissioninrole->name) == $permissioninrole->name ? 'selected' : '' }} value="{{$permissioninrole->id}}">{{$permissioninrole->name}}</option>
#endforeach
#isset($permissioninrole)
#foreach($permissions as $permission)
#if($permissioninrole->name != $permission->name)
<option class="rem" name="permissions[]" value="{{$permission->id}}">{{$permission->name}}</option>
#endif
#endforeach
#endisset
#empty($permissioninrole)
#foreach($permissions as $permission)
<option name="permissions[]" value="{{$permission->id}}">{{$permission->name}}</option>
#endforeach
#endempty
How it looks. HTML
<option name="permissions[]" selected value="15">role-create</option>
<option name="permissions[]" selected value="16">role-read</option>
<option name="permissions[]" value="15">role-create</option>
<option name="permissions[]" value="16">role-read</option>
...
How it should be if role 'x' has permission role-create.
<option name="permissions[]" selected value="15">role-create</option>
<option name="permissions[]" value="16">role-read</option>
...
Solution
#foreach($permissions as $key => $value)
<option name="permissions[]" {{ in_array($key +1, $role->permission()->pluck('id')->toArray()) ? 'selected' : '' }} value="{{$value->id}}">{{ $value->name }}</option>
#endforeach