2015-01-21 52 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();