Php 拉威尔的关系
我有两张桌子,Php 拉威尔的关系,php,orm,relationship,laravel,eloquent,Php,Orm,Relationship,Laravel,Eloquent,我有两张桌子,Gallery和Gallery\u items。 在图库中我保存了作者等信息。在Gallery\u items中,我保存库中包含的每张图片 现在我想在每个画廊中获得第一张图片,标题是蒙娜丽莎,作者是莱昂纳多·达芬奇 我测试了: Gallery_items::group_by('gallery_id')->where('title', '=', 'mona lisa')->gallery()->where('author', '=', 'Leonardo da Vi
Gallery
和Gallery\u items
。
在图库中
我保存了作者等信息。在Gallery\u items
中,我保存库中包含的每张图片
现在我想在每个画廊中获得第一张图片,标题是蒙娜丽莎,作者是莱昂纳多·达芬奇
我测试了:
Gallery_items::group_by('gallery_id')->where('title', '=', 'mona lisa')->gallery()->where('author', '=', 'Leonardo da Vinci');
但它不起作用。我收到一个错误查询类上未定义方法[gallery]。
但我已将gallery()添加到模型中
class Gallery_items extends Eloquent
{
public function gallery()
{
return $this->belongs_to('gallery');
}
}
你知道我该怎么做吗?
一个查询就可以做到这一点吗?
“可能是答案吗(我不明白那是什么)?这应该可以:
Gallery_items->group_by('gallery_id')->where_title_and_author('Mona Lisa', 'Leonardo da Vinci');
这应该起作用:
Gallery_items->group_by('gallery_id')->where_title_and_author('Mona Lisa', 'Leonardo da Vinci');
多亏了@AndHeiberg,我开始从其他角度看待这个问题,现在我用
JOIN
语句解决了这个问题
Gallery_items::join('gallery', 'Gallery_items.gallery_id', '=', 'gallery.id'))->group_by('gallery_id')->where_title_and_author('Mona Lisa', 'Leonardo da Vinci');
多亏了@AndHeiberg,我开始从其他角度看待这个问题,现在我用
JOIN
语句解决了这个问题
Gallery_items::join('gallery', 'Gallery_items.gallery_id', '=', 'gallery.id'))->group_by('gallery_id')->where_title_and_author('Mona Lisa', 'Leonardo da Vinci');
否,因为
Gallery\u items
没有名为author
的列。是Gallery
有该列。否,因为Gallery\u items
没有名为author
的列。正是库
有该列。