英文:
Laravel: Set old checked checkbox with pivot tables
问题
我不知道如何检查与数据透视表的“旧”值。我正在构建电子商务平台,每个产品类别可以有多个商店。在创建新类别时,我只选择一个商店,然后提交后选择的商店取消选中。
我的尝试
<!-- 仅在更新时显示,一切正常 -->
@if(isset($category))
@foreach($stores as $store)
<div class="form-check">
@if($category->stores()->pluck('store.id')->contains($store->id))
<input type="checkbox" name="stores[]" checked value="{{ $store->id }}">
@else
<input type="checkbox" name="stores[]" value="{{ $store->id }}">
@endif
<label class="form-check-label" for="category_status_field">{{ $store->name }}</label>
</div>
@endforeach
@else
<!-- 仅在创建时显示 -->
@foreach($stores as $store)
<div class="form-check">
<!-- 这里出现问题 -->
<input type="checkbox" name="stores[]" {{ old('stores') == $store->categories()->pluck('category.id')->contains($store->id) ? 'checked' : '' }} value="{{ $store->id }}">
<label class="form-check-label" for="category_status_field">{{ $store->name }}</label>
</div>
@endforeach
@endif
模型
class Store extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class, 'category_store', 'store_id');
}
}
class Category extends Model
{
public function stores()
{
return $this->belongsToMany(Store::class, 'category_store', 'category_id');
}
}
我还尝试过
<input type="checkbox" name="stores[]" {{ old('stores[$store->id]') == $store->id ? 'checked' : '' }} value="{{ $store->id }}">
我不明白如何检查哪个复选框已选中以及与什么匹配以显示“checked”。
谢谢。
英文:
I dont know how to check old
value with pivot tables. Am building ecommerce platform and each product category can have many stores. During creation new category i select only one store and after submit sleceted store is unchecked.
What i try
<!-- Show only on update and all works good -->
@if(isset($category))
@foreach($stores as $store)
<div class="form-check">
@if($category->stores()->pluck('store.id')->contains($store->id))
<input type="checkbox" name="stores[]" checked value="{{ $store->id }}">
@else
<input type="checkbox" name="stores[]" value="{{ $store->id }}">
@endif
<label class="form-check-label" for="category_status_field">{{ $store->name }}</label>
</div>
@endforeach
@else
<!-- Show only on create -->
@foreach($stores as $store)
<div class="form-check">
<!-- Here is problem -->
<input type="checkbox" name="stores[]" {{ old('stores') == $store->categories()->pluck('category.id')->contains($store->id) ? 'checked' : '' }} value="{{ $store->id }}">
<label class="form-check-label" for="category_status_field">{{ $store->name }}</label>
</div>
@endforeach
@endif
@endif
Models
class Store extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class, 'category_store', 'store_id');
}
}
class Category extends Model
{
public function stores()
{
return $this->belongsToMany(Store::class, 'category_store', 'category_id');
}
}
I also try
<input type="checkbox" name="stores[]" {{ old('stores[$store->id]') == $store->id ? 'checked' : '' }} value="{{ $store->id }}">
I dont understand how to check what checkbox is checked and with what to match to show checked
.
Thanks
答案1
得分: 2
你有一些问题,首先是一个N+1
问题,你在foreach()
循环的每次迭代中都加载了每个关联的store.id
。让我们首先解决这个问题。
在渲染此视图的控制器中,定义以下内容:
public function edit($categoryId) {
$category = Category::findOrFail($categoryId);
return view('categories.edit', [
'category' => $category,
'stores' => Store::all(), // 或类似的
'storeIds' => $category->stores->pluck('id')
]);
}
如果你正在使用Route Model Binding
,你可以跳过上面的步骤,直接这样做:
public function edit(Category $category) {
return view('categories.edit', [
'category' => $category,
'stores' => Store::all(), // 或类似的
'storeIds' => $category->stores->pluck('id')
]);
}
现在,你可以在视图中引用$storeIds
。然后,你可以使用@checked
属性(在Laravel 9.x
中可用):
@foreach($stores as $store)
<div class="form-check">
<input type="checkbox" name="stores[]" @checked((old() ? collect(old('stores', []))->contains($store->id) : $storeIds->contains($store->id))) value="{{ $store->id }}">
<label class="form-check-label" for="category_status_field">{{ $store->name }}</label>
</div>
@endforeach
这将检查old()
值(来自会话重定向)或者$storeIds
是否包含每个$store->id
,如果包含,将标记输入为checked
。
参考:
英文:
You've got a couple issues, the first of which is an N+1
issue, where you're loading every associated store.id
on every iteration of your foreach()
loop. Let's fix that first.
In your Controller that renders this view, define the following:
public function edit($categoryId) {
$category = Category::findOrFail($categoryId);
return view('categories.edit', [
'category' => $category,
'stores' => Store::all(), // or similar
'storeIds' => $category->stores->pluck('id')
]);
}
If you're using Route Model Binding
, you can skip that and simply do:
public function edit(Category $category) {
return view('categories.edit', [
'category' => $category,
'stores' => Store::all(), // or similar
'storeIds' => $category->stores->pluck('id')
]);
}
Now, you can reference $storeIds
in your view. You can then use the @checked
property (available in Laravel 9.x
)
@foreach($stores as $store)
<div class="form-check">
<input type="checkbox" name="stores[]" @checked((old() ? collect(old('stores', []))->contains($store->id) : $storeIds->contains($store->id))) value="{{ $store->id }}">
<label class="form-check-label" for="category_status_field">{{ $store->name }}</label>
</div>
@endforeach
This will check both the old()
values (from session redirect), or $storeIds
to see if it contains each $store->id
, and if it does, will mark the input as `checked.
For reference:
答案2
得分: 1
问题已解决
<input type="checkbox" name="stores[]" {{ is_array(old('stores')) && in_array($store->id, old('stores')) ? 'checked ' : '' }} value="{{ $store->id }}">
英文:
Problem sloved
<input type="checkbox" name="stores[]" {{ is_array(old('stores')) && in_array($store->id, old('stores')) ? 'checked ' : '' }} value="{{ $store->id }}">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论