如何做到这一点加入查询laravel方式

时间:2016-05-02 09:14:49

标签: php mysql laravel eloquent laravel-5.2

    SELECT listings.id, 
       listings.space_fullname, 
       images.image_file 
    FROM   listings 
       LEFT OUTER JOIN images 
                    ON images.id = (SELECT id 
                                    FROM   images 
                                    WHERE  images.listings_id = listings.id 
                                    LIMIT  1) 
    WHERE  listings.id = 69 

如何在不使用DB :: raw或DB :: select?

的情况下以laravel方式执行此查询

谢谢

1 个答案:

答案 0 :(得分:1)

->I think listings and images are two different tables
->In both tables  images.listings_id and  listings.id may be equal

然后在查询下面可能有用

 DB::table('listings')->leftjoin('images','images.listings_id','=','listings.id')            ->select('listings.id','listings.space_fullname','images.image_file')
            ->where('listings.id','=',69)
            ->get();