Php Laravel雄辩:如何按产品类别和/或产品品牌筛选产品评论?

Php Laravel雄辩:如何按产品类别和/或产品品牌筛选产品评论?,php,laravel,eloquent,Php,Laravel,Eloquent,我将更详细地描述这种情况: 我有一份产品清单,其中每种产品都属于某一类别和某一品牌。一些产品可以得到用户的审查 在我的Laravel应用程序的/reviews/page上,我有一个评论列表,为类别和品牌选择框,当然还有搜索按钮 若用户并没有选择类别或品牌,那个么所有的评论都会被显示、分页,这很好 当用户选择类别或品牌或两者中的任何一个,并试图以这种方式过滤所有评论时,问题就会出现 Reviews table fields: ID, user_id(foreign key users.ID), p

我将更详细地描述这种情况:

我有一份产品清单,其中每种产品都属于某一类别和某一品牌。一些产品可以得到用户的审查

在我的Laravel应用程序的/reviews/page上,我有一个评论列表,为类别和品牌选择框,当然还有搜索按钮

若用户并没有选择类别或品牌,那个么所有的评论都会被显示、分页,这很好

当用户选择类别或品牌或两者中的任何一个,并试图以这种方式过滤所有评论时,问题就会出现

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);
如果我想按用户id筛选评论,那将很容易,因为评论表包含用户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');
    }
}
这用于按类别\u id进行过滤:

if (Input::has('category_id')){
    $category_id = Input::get('category_id');
    $reviews_query->where('categories.id', $category_id);
}
我不知道如何正确处理刀片模板中的模糊id,如美国产品id、审查id、用户id($review->id、$review->user->id,所有这些都是相互混淆的)

试试这个

$reviews = DB::table('review')
->join('product', 'product.id', '=', 'review.product_id')
->Join('category', 'product.category_id', '=', 'category.id')
->orderBy('id', 'DESC')->paginate(5);
有关更多信息,请访问-


这可能适用于您….

在您的类别模型中添加hasManyThrough关系

public function reviews() {
    return $this->hasManyThrough('App\Review', 'App\Product');
}
现在,您可以按如下类别查看所有评论

$category->reviews();
您可以向其中添加其他查询子句,如

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

按特定类别ID筛选所有评论,然后只需使用->

$categoryID = 1;
Categories::with(['products' => function($product){
        $product->with('reviews');
    }])->where('id', '=', $categoryID)->get();
还可以按特定的BrandID筛选所有评论,然后

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

你在哪里存储评论…?这是什么意思?在评论数据库表中,只需检查特定产品评论的存储位置….?您只能检索那些产品我不需要产品,我需要某个类别中所有产品的评论这很好,但是在刀片模板中出现错误$review->id,这解决了刀片模板中$review->id错误的问题,这很好。我想知道如何根据Input::get('category\u id')对评论进行子筛选?你知道吗?
$brandID = 1;
Brands::with(['products' => function($product){
        $product->with('reviews');
    }])->where('id', '=', $brandID)->get();