2015-01-21 53 views
0

我有三个表。雄辩的如何省略行如果值为空

  1. 客户
  2. 产品
  3. 评论
  4. 我用雄辩的映射这些表

例如,

在审查模式为客户详细信息我有一个像

public function customer(){ 
     return $this->belongsTo('Customer', 'customer_id'); 
} 

和产品细节功能我有功能类似

public function product(){ 
     return $this->belongsTo('Product', 'product_id'); 
} 

现在查询评论模型像

Review::all()->with(array('customer', 'product'))->get() 

返回值。没事儿。但是,如果任何客户被删除,那么该行的值就是空的。相反,我需要省略那一行。如何在laravel中做到这一点。

回答

0

试试这个......也许在你的方法中加入where子句也可以。

$allReviews = Review::all() 
    ->join('customers', 'reviews.customer_id', '=', 'customers.id') 
    ->join('products', 'reviews.product_id', '=', 'products.id') 
    ->where('customers.firstname','!=','') // given that there is a column firstname in table customers 
    ->get() 
0

您可以使用has()来筛选具有车型至少一个相关型号:

Review::with('customer', 'product')->has('customer')->get();