渴望加载没有空集合

dom*_*zaq 1 eloquent laravel-4

$products = Product::with(
    array(
        'productDescriptions' => function($query) {
             $query->lang(App::getLocale());
         },
        'productImages' => function($query) {
             $query->imageType('thumbnail');    
        }))
    ->paginate($items_per_page);
Run Code Online (Sandbox Code Playgroud)

嗨,有没有一种简单的方法来加载关系,但只有它们存在?更具体地说,如果他们的productDescriptions不是空的,我想只获得这些产品.在SQL中我只是做一个内部联接,但是如何在Eloquent中轻松实现呢?我可以预先检查所有退回的产品并检查是否$product->productDescription->count() > 0但是可能有更简单的方法来实现这一目标.

Jar*_*zyk 6

有两种方法:haswhereHas:

$products = Product::with(   // this part will load the relation
  array(
    'productDescriptions' => function($query) {
         $query->lang(App::getLocale());
     },
    'productImages' => function($query) {
         $query->imageType('thumbnail');    
    }))
  ->has('productDescriptions') // this will make sure only Products having related descriptions will be fetched
  ->paginate($items_per_page);
Run Code Online (Sandbox Code Playgroud)