Laravel 4:如何使用Query Builder进行子查询?

时间:2015-11-25 09:28:10

标签: mysql laravel laravel-4 query-builder

我有两张桌子。有一个表地方,其中包含如下所示的数据:

id   name 
-----------
1    A1     
2    A2     
3    A3      
4    B1       

其他表 place_conditions 包含如下所示的数据:

id   placeID     desription
----------------------------
1    1           Windy     
2    1           Dry     
3    3           Wet      
4    3           Rainy
4    4           Dry

我喜欢在laravel 4中进行查询,结果如下:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => A1
            [place_cond] => Array
                               (
                               [0] => stdClass Object
                                    (
                                       [description] => Windy
                                    )
                               [1] => stdClass Object
                                    (
                                       [description] => Dry
                                    )
                               )
        )
    [1] => stdClass Object
        (
            [id] => 2
            [name] => A2
            [place_cond] => Array
                               (

                               )
        )
    [3] => stdClass Object
        (
            [id] => 3
            [name] => A3
            [place_cond] => Array
                               (
                               [0] => stdClass Object
                                    (
                                       [description] => Wet
                                    )
                               [1] => stdClass Object
                                    (
                                       [description] => Rainy
                                    )
                               )
        )
)

这是我的部分查询(仍然是错误):

    $query = DB::table('place');
    if(Input::has('search')){
       $query->where(function($a){
          $a->where('name','LIKE','%'.Input::get('search').'%');
       });
    }
   /* $query->select('place.*',DB::raw('SELECT Description as place_cond FROM place_conditions WHERE place_conditions.placeID = place.id')); // sub query . Arrggghhhhhh... whyyyy.. stillllll.. errorrrrrrr.. */
    $data = $query->get();

是否可以在laravel 4中产生如上所述的结果?你能帮助我吗?谢谢

0 个答案:

没有答案