我有一个餐桌产品:
表类别:
在addProduct视图中,我想显示一个包含类别表中类别的选择框:
但我有这个错误
未定义的变量:类别(查看: C:\ XAMPP \ htdocs中\ BLG \资源\视图\ addProduct.blade.php)
addProduct.balde.php
<div class="form-group">
<label for="inputPassword" class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" name ="name" class="form-control" id="" placeholder="">
</div>
</div>
<div class="form-group">
<label for="textArea" class="col-lg-2 control-label">categorie</label>
<div class="col-lg-10">
@foreach($categories as $categorie)
<option value="{{ $categorie->id }}">{{ $categorie->name }}</option>
@endforeach
</div>
</div>
CreatesController.php
public function add(Request $request)
{
$categories = Categories::all();
$products = new Product;
$products->name = $request->input('name');
$products->date_operation = $request->input('date_operation');
$products->category_id = $request->input('category_id');
$products->save();
return view('addProdcut', ['categories' => $categories]);
}
答案 0 :(得分:0)
你的结构不正确。
你的方法必须是那个
public function create() {
$categories = Categories::all();
return view('addProdcut', ['categories' => $categories]);
}
public function add(Request $request)
{
$products = new Product;
$products->name = $request->input('name');
$products->date_operation = $request->input('date_operation');
$products->category_id = $request->input('category_id');
$products->save();
return redirect('some page')->withMessage('your product has been added');
}