如何设置默认值 Laravel <select> 元素?
How can I set the default value Laravel <select> element?
我想检索类别名称中的数据库值,我想在选择中显示默认值。这是我的编辑视图控制器。
我有一个 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);
}
我收到这个错误。
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id
您需要在 foreach()
循环上方的 select 标记中添加默认值。
<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>
如果要设置类别名称 selected 则需要匹配产品集合中的类别。 (如果您在产品 table 中有 catetory_id
)
<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>
我想检索类别名称中的数据库值,我想在选择中显示默认值。这是我的编辑视图控制器。
我有一个 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);
}
我收到这个错误。
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id
您需要在 foreach()
循环上方的 select 标记中添加默认值。
<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>
如果要设置类别名称 selected 则需要匹配产品集合中的类别。 (如果您在产品 table 中有 catetory_id
)
<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>