未定义的变量:类别(视图:resources\views\product\create.blade.php)
Undefined variable: categories (View: resources\views\product\create.blade.php)
您好,我试图在 products/create 页面中获取类别,但它显示:(查看:\resources\views\product\create。blade.php)如何解决?
控制器:
public function create(Request $request)
{
$categories = Category::with('category')->distinct()->get();
return view('product.create', compact('categories'));
}
blade 文件:
<select name="select">
@foreach($categories as $category)
<option value="{{ $id }}">{{ $title }}</option>
@endforeach
</select>
类别模型:
public function products()
{
return $this->belongsToMany(Product::class,'category_product');
}
产品型号:
public function categories()
{
return $this->belongsToMany(Category::class,'category_product');
}
这些是路线:
| | | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest |
| | POST | product | product.store | App\Http\Controllers\ProductController@store | web |
| | GET|HEAD | product | product.index | App\Http\Controllers\ProductController@index | web |
| | GET|HEAD | product/create | product.create | App\Http\Controllers\ProductController@create | web |
| | DELETE | product/{product} | product.destroy | App\Http\Controllers\ProductController@destroy | web |
| | GET|HEAD | product/{product} | product.show | App\Http\Controllers\ProductController@show | web |
| | PUT|PATCH | product/{product} | product.update | App\Http\Controllers\ProductController@update | web |
| | GET|HEAD | product/{product}/edit | product.edit | App\Http\Controllers\ProductController@edit | web |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
如果你想检索类别列表,你只需要像这样更改你的代码,这样你就会得到正确的所有类别
public function create(Request $request)
{
$categories = Category::distinct()->get(); // or Category::all();
return view('product.create', compact('categories'));
}
如果你对你的错误感到好奇,你不需要 with ,如果你想检索所需的关系(预先加载),可以使用此方法
根据您的 blade,您不需要它,因为您想要检索类别列表
您好,我试图在 products/create 页面中获取类别,但它显示:(查看:\resources\views\product\create。blade.php)如何解决?
控制器:
public function create(Request $request)
{
$categories = Category::with('category')->distinct()->get();
return view('product.create', compact('categories'));
}
blade 文件:
<select name="select">
@foreach($categories as $category)
<option value="{{ $id }}">{{ $title }}</option>
@endforeach
</select>
类别模型:
public function products()
{
return $this->belongsToMany(Product::class,'category_product');
}
产品型号:
public function categories()
{
return $this->belongsToMany(Category::class,'category_product');
}
这些是路线:
| | | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest |
| | POST | product | product.store | App\Http\Controllers\ProductController@store | web |
| | GET|HEAD | product | product.index | App\Http\Controllers\ProductController@index | web |
| | GET|HEAD | product/create | product.create | App\Http\Controllers\ProductController@create | web |
| | DELETE | product/{product} | product.destroy | App\Http\Controllers\ProductController@destroy | web |
| | GET|HEAD | product/{product} | product.show | App\Http\Controllers\ProductController@show | web |
| | PUT|PATCH | product/{product} | product.update | App\Http\Controllers\ProductController@update | web |
| | GET|HEAD | product/{product}/edit | product.edit | App\Http\Controllers\ProductController@edit | web |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
如果你想检索类别列表,你只需要像这样更改你的代码,这样你就会得到正确的所有类别
public function create(Request $request)
{
$categories = Category::distinct()->get(); // or Category::all();
return view('product.create', compact('categories'));
}
如果你对你的错误感到好奇,你不需要 with ,如果你想检索所需的关系(预先加载),可以使用此方法
根据您的 blade,您不需要它,因为您想要检索类别列表