我有两个网上商店的 mySQL 表:
产品(列:id 等)
图片(列:id、product_id、文件路径等...)
产品表中的任何产品都可能有无限数量的图片!
因此,我的产品模型如下所示:
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)
备注:Filepath是一列图片。我试图在刀片中访问它,如下所示:
{{$products->picture->filepath}}
我现在在这个小事情上工作了几个小时 - 但我只是不明白我做错了什么?非常感谢任何帮助!感谢你们!
一只甜甜圈