Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-基于类别显示产品,但不能在刀片文件中显示类别名称_Php_Laravel - Fatal编程技术网

Php Laravel-基于类别显示产品,但不能在刀片文件中显示类别名称

Php Laravel-基于类别显示产品,但不能在刀片文件中显示类别名称,php,laravel,Php,Laravel,拉威尔需要帮助 我正在尝试展示基于类别的产品&我已经做到了。我需要在产品视图文件中显示类别名称,但无法显示 创建表 具有id、pname、cid的产品 id为cname的类别 路由文件:Route::get'/categories/{id}','CategoriesController@show'; 路由::获取“/categories/”,'CategoriesController@index'; 我的控制器 我的看法 我想在类别模型中的产品列表上方显示类别名称添加关系: public fun

拉威尔需要帮助

我正在尝试展示基于类别的产品&我已经做到了。我需要在产品视图文件中显示类别名称,但无法显示

创建表

具有id、pname、cid的产品

id为cname的类别

路由文件:Route::get'/categories/{id}','CategoriesController@show'; 路由::获取“/categories/”,'CategoriesController@index';

我的控制器

我的看法


我想在类别模型中的产品列表上方显示类别名称添加关系:

public function products(){
   return $this->hasMany('App\Product','cid');
}
在控制器中,获取所有类别并加载您的产品:

$categories=Category::with('products')->get();
return view ('product',compact('categories'));
在您的刀片视图中:

@foreach ($categories as $category)
    {{$category->cname}}
    @foreach ($category->products as $product)
        {{$product->pname}}
    @foreach
@foreach

在类别模型中添加关系:

public function products(){
   return $this->hasMany('App\Product','cid');
}
在控制器中,获取所有类别并加载您的产品:

$categories=Category::with('products')->get();
return view ('product',compact('categories'));
在您的刀片视图中:

@foreach ($categories as $category)
    {{$category->cname}}
    @foreach ($category->products as $product)
        {{$product->pname}}
    @foreach
@foreach

第一件事,我建议使用渴望加载。请看一看您的产品和类别表。此外,如果名称正确,则应为$product->category->category\u name您可能有不同的名称,但需要从product调用category relationship您的模型中设置了关系吗?@christophert in category Model public function product{return$this->hasManyProduct::class;}在产品模型公共功能类别{return$this->belongsTocategory::class;}@Mohamed中,请检查答案-如果它解决了您的问题,请不要忘记首先接受它,我建议使用渴望加载。请看一看您的产品和类别表。此外,如果名称正确,则应为$product->category->category\u name您可能有不同的名称,但需要从product调用category relationship您的模型中设置了关系吗?@christophert in category Model public function product{return$this->hasManyProduct::class;}在产品模型公共功能类别中{return$this->belongsTocategory::class;}@Mohamed请检查答案-如果它解决了您的问题,请不要忘记接受它,谢谢。像你们这样留下问题答案的人会让Stack Overflow成为一个更好的地方。笑脸表情符号。谢谢。像你们这样留下问题答案的人会让Stack Overflow成为一个更好的地方。微笑表情符号。