我有两张桌子,Galleries
和Gallery_items
。在Galleries
我保存诸如作者是谁之类的信息。在Gallery_items
我保存画廊包含的每张图片。
现在我想在每个画廊中获取第一张照片,其中标题是蒙娜丽莎,作者是莱昂纳多·达·芬奇。
我测试过:
Gallery_items::group_by('gallery_id')->where('title', '=', 'mona lisa')->gallery()->where('author', '=', 'Leonardo da Vinci');
但它不起作用。我得到一个错误Method [gallery] is not defined on the Query class.
。
但我已将 gallery() 添加到模型中。
class Gallery_items extends Eloquent
{
public function gallery()
{
return $this->belongs_to('gallery');
}
}
知道我该怎么做吗?这甚至可以通过一个查询来实现吗?“约束急切加载”也许是答案(我不明白那是什么)?