【发布时间】:2020-11-08 03:30:01
【问题描述】:
我有两个网店的mySQL表:
产品(列:id 等...)
图片(列:id、product_id、文件路径等...)
products-table 的任何 product 可能有无限数量的 图片!
因此,我的产品模型如下所示:
class Product extends Model
{
public function pictures()
{
return $this->hasMany('App\Picture');
}
}
我的图片模型是这样的:
class Picture extends Model
{
//
}
现在,我正在尝试列出所有产品一起及其相应的图片数据集。我尝试运行多个代码。但是,以下代码均无效:
\App\Product::pictures()->get()
不起作用!显示此错误:
Non-static method App\Product::pictures() should not be called statically
我也尝试了一些其他的方法,例如
\App\Product::with('pictures')->get());
错误:
Trying to get property 'filepath' of non-object (View: /home/vagrant/testproject/resources/views/products.blade.php)
备注:文件路径是一列图片。我正在尝试像这样在刀片中访问它:
{{$products->picture->filepath}}
我现在在这个小东西上工作了几个小时 - 但我就是不明白我做错了什么?非常感谢任何帮助!谢谢各位!
【问题讨论】: