简体   繁体   English

Laravel Eloquent:如何按产品类别和/或产品品牌过滤产品评论?

[英]Laravel Eloquent: how to filter reviews of products by product category and/or product brand?

I will describe this situation with more details:我将更详细地描述这种情况:

I have a list of products where every product belongs to a certain category and to a certain brand.我有一个产品列表,其中每个产品都属于某个类别和某个品牌。 Some of the products can get reviewed by users.一些产品可以得到用户的审查。

On /reviews/ page in my Laravel application, I have a list of reviews and select boxes for category and brand along with search button of course.在我的 Laravel 应用程序的 /reviews/ 页面上,我有一个评论列表,当然还有类别和品牌的选择框以及搜索按钮。

If user doesn't choose category or brand, all reviews get displayed, paginated, and that's good.如果用户不选择类别或品牌,所有评论都会显示、分页,这很好。

The problem arises when user chooses either category or brand or both and tries to get all the reviews filtered that way.当用户选择类别或品牌或两者并尝试以这种方式过滤所有评论时,就会出现问题。

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

When I'm listing reviews, I'm simply using:当我列出评论时,我只是使用:

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

If I would like to filter reviews by user_id, that would be easy as the reviews table contains user_id column, but, how to filter them by product category and/or product brand?如果我想按 user_id 过滤评论,这很容易,因为评论表包含 user_id 列,但是,如何按产品类别和/或产品品牌过滤它们?

Here are Review, Product and Category models:以下是评论、产品和类别模型:

<?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');
    }
}

If I use joins, then $review->user->id, $review->user->username, $review->id are not correct, I'm getting reviews.product_id as $review->id, and products.user_id as $review->user->id in blade template.如果我使用连接,那么 $review->user->id、$review->user->username、$review->id 是不正确的,我将得到 review.product_id 作为 $review->id 和产品。 user_id 作为刀片模板中的 $review->user->id。

I was trying this join variant:我正在尝试这个连接变体:

$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);

And this for filtering by category_id:这是按 category_id 过滤的:

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

I'm not sure how to correctly address ambiguous ids such us product_id, review_id, user_id in blade template ($review->id, $review->user->id, all are messed up mutually)我不知道如何在刀片模板中正确处理模棱两可的 id,例如我们的 product_id、review_id、user_id($review->id、$review->user->id,所有这些都是相互混淆的)

Add hasManyThrough relationship in your category model在您的类别模型中添加 hasManyThrough 关系

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

now you can have all reviews by a category like this现在您可以按这样的类别查看所有评论

$category->reviews();

you can add other query clauses to it like您可以向其添加其他查询子句,例如

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

Try this,尝试这个,

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

for more you can visit- Laravel join with 3 Tables更多你可以访问 - Laravel 加入 3 个表

This may work for you.....这可能对你有用......

filter all reviews by a specific CategoryID then simply use ->按特定 CategoryID 过滤所有评论,然后只需使用 ->

$categoryID = 1;
Categories::with(['products' => function($product){
        $product->with('reviews');
    }])->where('id', '=', $categoryID)->get();

also filter all reviews by a specific BrandID then还可以通过特定的 BrandID 过滤所有评论,然后

$brandID = 1;
Brands::with(['products' => function($product){
        $product->with('reviews');
    }])->where('id', '=', $brandID)->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM