【问题标题】:Display Category Name With Category ID from Product - Laravel使用产品中的类别 ID 显示类别名称 - Laravel
【发布时间】:2021-09-24 03:40:38
【问题描述】:

我已经找到了这个答案,但它对我不起作用 (Laravel - displaying categories by id)

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

我的数据库:

类别: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 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>

我得到一个错误:

Trying to get property of non-object (View: C:\xampp\htdocs\banhang\resources\views\admin\products\danhsach.blade.php)

我错在哪里?请告诉我如何解决它。

谢谢

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您将通过Category() 关系方法获得CategoryName

    <td>{{$sp->Category()->first()->CategoryName}}</td>
    

    或更短:

    <td>{{$sp->Category->CategoryName}}</td>
    

    编辑:您的关系方法使用了错误的外键。试试:

    产品型号:

    public function Category() 
    {
        return $this->belongsTo('App\Category', 'ProductCategoryID', 'CategoryID');
    }
    

    Category模型(注意方法的复数命名:Products):

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

    【讨论】:

    • 您好@Baik Ho,我已经这样做了,但仍然出现错误:未定义的属性:Illuminate\Database\Eloquent\Relations\BelongsTo::$CategoryName(查看:C:\xampp\htdocs\ banhang\resources\views\admin\products\danhsach.blade.php) :( 我错了:(
    • 我已经更新了答案。忘记了在不使用动态属性时必须调用first()。请再看看。
    • 我做到了,但仍然没有工作。我仍然收到错误“尝试获取非对象的属性”。不知道我的Controller对不对?
    • ProductCategoryID 可以在您的设置中使用 null 吗?
    • 我已经解决了这个问题。我将控制器更改为 $sanpham = Product::with('Category')->get();我工作了。非常感谢@baik-ho
    【解决方案2】:

    我想根据 Laravel 8 更新答案。

    产品型号:

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

    类别模型:

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

    【讨论】:

      【解决方案3】:

      替换:$sanpham = Product::all();
      作者:$sanpham = Product::with('category')-&gt;get(); 那么在你看来:$sp-&gt;category-&gt;CategoryName

      优化的最佳答案(查询次数):

      $sp->category->CategoryName
      
      
      
       
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        • 2019-08-09
        • 1970-01-01
        • 2020-08-16
        相关资源
        最近更新 更多