2013-01-11 68 views
0

我有两个表,GalleriesGallery_items。 在Galleries我保存作者是谁的信息。在Gallery_items我保存该图库包含的每张图片。Laravel中的关系

现在我想获取的第一张图片中每个画廊其中标题是蒙娜丽莎并在作者是达芬奇

我测试:

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'); 
    } 
} 

任何想法我应该怎么做? 这甚至有可能与一个查询? “Constraining Eager Loading”也许是答案(我不明白那是什么)?

回答

2

由于@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'); 
1

这应该工作:

Gallery_items->group_by('gallery_id')->where_title_and_author('Mona Lisa', 'Leonardo da Vinci'); 
+0

否,因为'Gallery_items'没有名为'author'的列。这是'画廊'有那一栏。 – Sawny