2015-07-06 70 views
2

我會更詳細地描述這種情況:Laravel Eloquent:如何按產品類別和/或產品品牌過濾產品評論?

我有一個產品列表,其中每個產品屬於某個類別和某個品牌。一些產品可以被用戶查看。

在我的Laravel應用程序的開啓/評論/頁面上,我有一個評論和選擇框的類別和品牌的列表,當然還有搜索按鈕。

如果用戶沒有選擇分類或品牌,所有評論都會顯示,分頁,這很好。

問題出現在用戶選擇任一類別或品牌或兩者並試圖獲得以這種方式過濾的所有評論。

Reviews table fields: ID, user_id(foreign key users.ID), product_id(foreign key products.ID), text 
Products table fields: ID, category_id(foreign key categories.ID), brand_id(foreign key brands.ID), name 
Categories table fields: ID, name 
Brands table fields: ID, name 
Users table fields: ID, username 

當我列出的評論,我只是使用:

$reviews = Review::orderBy('id', 'DESC')->paginate(5); 

如果我想過濾的USER_ID審查,審查表包含user_id列,這將是容易的, 但是,如何按產品類別和/或產品品牌過濾它們?

以下是評論,產品和類別車型:

<?php namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Review extends Model { 
    protected $fillable = []; 

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

    public function user() { 
     return $this->belongsTo('App\User'); 
    } 
} 

<?php namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Product extends Model { 

    protected $fillable = []; 

    public function user() { 
     return $this->belongsTo('App\User'); 
    } 

    public function reviews() { 
     return $this->hasMany('App\Review'); 
    } 

    public function category() { 
     return $this->belongsTo('App\Category'); 
    } 

} 


<?php namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Category extends Model { 
    protected $fillable = []; 

    public function products() { 
     return $this->hasMany('App\Product'); 
    } 
} 

如果我使用的連接,然後$審查 - >用戶>的id,$審查 - >用戶>用戶名,$審查 - > ID是不正確,我在刀片模板中將reviews.product_id作爲$ review-> id和products.user_id作爲$ review-> user-> id。

我嘗試這個連接的變體:

$reviews_query = Review::orderBy('reviews.id', 'DESC') 
      ->Join('products', 'reviews.product_id', '=', 'products.id') 
      ->Join('categories', 'products.category_id', '=', 'categories.id') 
      ->Join('brands', 'products.brand_id', '=', 'brands.id') 
      ->Join('users', 'reviews.user_id', '=', 'users.id') 
      ->where('reviews.active', '=', 1) 
      ->GroupBy('reviews.id')->paginate(5); 

而本作由CATEGORY_ID過濾:

if (Input::has('category_id')){ 
    $category_id = Input::get('category_id'); 
    $reviews_query->where('categories.id', $category_id); 
} 

我不知道如何正確地解決曖昧IDS這樣我們的product_id,REVIEW_ID,USER_ID在刀片模板中($ review-> id,$ review-> user-> id,全部相互混淆)

+0

添加其他查詢子句給它你在哪裏存儲評價......? –

+0

你是什麼意思?在評論數據庫表 – mpet

+0

所以只是檢查在哪裏存儲特定的產品評論???你只能檢索那些產品 –

回答

0

試試這個,

$reviews = DB::table('review') 
->join('product', 'product.id', '=', 'review.product_id') 
->Join('category', 'product.category_id', '=', 'category.id') 
->orderBy('id', 'DESC')->paginate(5); 

更多,你可以visit- Laravel join with 3 Tables

這可能會爲你工作.....

+0

那樣會是好的,但讓我錯誤的刀片模板,我得到錯誤$ review->編號 – mpet

+1

您將在指定的鏈接中得到很好的參考... –

2

添加hasManyThrough關係的分類模型

public function reviews() { 
    return $this->hasManyThrough('App\Review', 'App\Product'); 
} 

現在你可以有這樣的類別的所有評論

$category->reviews(); 

你可以像

$category->reviews()->orderBy('id', 'DESC')->paginate(5); 
+0

這解決了錯誤$ review-> ID在刀片模板中的問題,並且很好。我在徘徊如何基於Input :: get('category_id)來進行子過濾評論?你有什麼主意嗎? – mpet