Php 显示类别名称以及产品-Laravel中的类别ID

Php 显示类别名称以及产品-Laravel中的类别ID,php,laravel,Php,Laravel,我已经找到了这个答案,但它对我不起作用() 我无法在产品列表页面中显示类别名称 我的数据库: 类别:CategoryID,CategoryName 产品:ProductID、名称、Des、ProductCategoryID 产品型号: protected $table ='products'; protected $primaryKey = 'ProductID'; public $timestamps = false; public function Category(){ ret

我已经找到了这个答案,但它对我不起作用()

我无法在产品列表页面中显示类别名称

我的数据库:

类别:CategoryID,CategoryName

产品:ProductID、名称、Des、ProductCategoryID

产品型号:

protected $table ='products';

protected $primaryKey = 'ProductID';

public $timestamps = false;

public function Category(){
   return $this->belongsTo('App\Category','ProductID','CategoryID');
}
类别模型:

protected $table = 'productcategories';

public function Product(){
  return $this->hasMany('App\Product','ProductCategoryID','ProductID');
}
public function Products()
{
    return $this->hasMany('App\Models\Product', 'ProductCategoryID');
}
我的控制器:

 public function danhsachsanpham(){

$sanpham = Product::all();
return view('admin/products/danhsach', ['sanpham'=> $sanpham]);
}

我的看法:

            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
            <thead>
                <tr align="center">
                    <th>ID</th>
                    <th>Name</th>
                    <th>Description</th>
                    <th>Category Name</th>                        
                </tr>
            </thead>
            <tbody>
            <?php foreach ($sanpham as $sp): ?>
                 <tr class="odd gradeX" align="center">
                    <td>{{$sp->ProductID}}</td>
                    <td>{{$sp->ProductName}}</td>
                    <td>{{$sp->ProductLongDesc}}</td>
                    <td>{{$sp->ProductCategoryID->CategoryName}}</td>
                </tr>
            <?php endforeach ?>
            </tbody>
        </table>
我哪里错了?请告诉我如何修理它


谢谢

您可以通过
Category()
关系方法获得类别名称

<td>{{$sp->Category()->first()->CategoryName}}</td>
类别型号(注:方法名称:产品s):


您可以通过
Category()
relationship方法获得CategoryName

<td>{{$sp->Category()->first()->CategoryName}}</td>
类别型号(注:方法名称:产品s):


我想根据Laravel 8更新答案

产品型号:

public function Category() 
{
    return $this->belongsTo('App\Models\Category', 'ProductCategoryID', 'CategoryID');
}
类别模型:

protected $table = 'productcategories';

public function Product(){
  return $this->hasMany('App\Product','ProductCategoryID','ProductID');
}
public function Products()
{
    return $this->hasMany('App\Models\Product', 'ProductCategoryID');
}

我想根据Laravel 8更新答案

产品型号:

public function Category() 
{
    return $this->belongsTo('App\Models\Category', 'ProductCategoryID', 'CategoryID');
}
类别模型:

protected $table = 'productcategories';

public function Product(){
  return $this->hasMany('App\Product','ProductCategoryID','ProductID');
}
public function Products()
{
    return $this->hasMany('App\Models\Product', 'ProductCategoryID');
}

您好@Baik Ho,我已经这么做了,但是我仍然得到了一个错误:未定义的属性:illumb\Database\elount\Relations\BelongsTo::$CategoryName(视图:C:\xampp\htdocs\banhang\resources\views\admin\products\danhsach.blade.php):(我错了:(I'v更新了答案。忘了您必须先调用
first()
当不使用动态属性时。请再看一次。我这样做了,但仍然没有工作。我仍然收到一个错误“尝试获取非对象的属性”。我不知道我的控制器是否正确?ProductCategoryID在您的设置中是否可以
null
?我已经修复了这个问题。我将控制器更改为$sanpham=Product::with('Category'))->get();我工作了。非常感谢@baik hoHello@baik Ho,我已经这么做了,但是我仍然遇到了一个错误:未定义的属性:illumb\Database\Eloquent\Relations\BelongsTo::$CategoryName(视图:C:\xampp\htdocs\banhang\resources\views\admin\products\danhsach.blade.php)(我错在哪里:(我更新了答案。忘记了在不使用动态属性时必须先调用
first()
。请再看一看。我这样做了,但仍然无法工作。我仍然收到一个错误“尝试获取非对象的属性”。我不知道我的控制器是否正确?ProductCategoryID在您的设置中是否可以为
null
?我已经解决了这个问题。我将我的控制器更改为$sanpham=Product::with('Category')->get();我已经工作了。非常感谢@baik ho