简体   繁体   English

如何在Laravel中拿出我所有的产品并平均评价该产品?

[英]How can I pull out all my products and average the reviews on the product in Laravel?

$product = DB::table('product_list')->join('product_categories', 'product_list.product_category', '=', 'product_categories.id')
    ->join('product_types', 'product_list.product_type', '=', 'product_types.id')
    ->where('product_list.id', (int) $productId)
    ->get()
    ->first();

Which gets the correct product that is passed through the route 哪个获得通过路线传递的正确产品

Route::get('/products/{productId}', 'ProductController@single')->name('Single Product');

The issue is that I have a product_reviews table which has a relation to the product_list table. 问题是我有一个product_reviews表,该表与product_list表有关系。

public function up()
{
    Schema::create('product_reviews', function(Blueprint $table) {
        $table->increments('id');

        $table->integer('user_id')->unsigned();
        $table->integer('product_id')->unsigned();

        $table->string('review')->default('No comment was made on this product.');
        $table->integer('stars')->default(0);

        $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
        $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

        $table->index(array('user_id', 'product_id'));

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('product_id')->references('id')->on('product_list');
    });
}

The users reviews are stored in a separate table which the product_id relates to the id of the $product I have got from the DB. 用户评论存储在单独的表中,其中product_id与我从数据库获得的$productid有关。

I am trying to get the AVG() rating from all these stars based on the product ID. 我正在尝试根据产品ID从所有这些stars获得AVG()等级。 I have tried 我努力了

$stars = DB::table('product_reviews')->selectRaw('AVG(stars)')
    ->whereColumn('product_id', 'product_list.id');

$product = DB::table('product_list')->join('product_categories', 'product_list.product_category', '=', 'product_categories.id')
    ->join('product_types', 'product_list.product_type', '=', 'product_types.id')
    ->selectSub($stars, 'stars_avg')
    ->where('product_list.id', (int) $productId)
    ->get()
    ->first();

But I am receiving this output 但是我收到了这个输出

{#631 ▼
  +"stars_avg": 5.0000
}

Rather than all my product information, with an extra added stars_avg column. 除了增加我的所有产品信息外,还添加了额外的stars_avg列。 How can I bring out all my inner joins with this extra column on top? 如何通过顶部的此额外列显示所有内部联接?

I managed to fix this by first querying my data before selecting the subquery. 我设法通过选择子查询之前先查询我的数据来解决此问题。 I also added ROUND() to the query to stop null encounters and to round the value to the nearest 10. 我还向查询中添加了ROUND() ,以防止遇到null值并将值四舍五入到最接近的10。

$stars = DB::table('product_reviews')->selectRaw('ROUND(AVG(stars))')
        ->whereColumn('product_id', 'product_list.id');

$product = DB::table('product_list')->join('product_categories', 'product_list.product_category', '=', 'product_categories.id')
        ->select('*')
        ->selectSub($stars, 'stars_avg')
        ->join('product_types', 'product_list.product_type', '=', 'product_types.id')
        ->where('product_list.id', (int) $productId)
        ->take(1)
        ->get()
        ->first();

This now gives me my desired output of: 现在,这给了我我想要的输出:

{#634 ▼
  +"id": 3
  +"cost": "150.00"
  +"product_category": 1
  +"product_type": 3
  +"product_score": 0
  +"created_at": "2019-01-16 16:34:29"
  +"updated_at": "2019-01-16 16:34:29"
  +"rating": 0
  +"down_payment": "10.00"
  +"title": "Static"
  +"price_start": "50.00"
  +"price_stop": "150.00"
  +"theme": "Custom"
  +"pages": 4
  +"rbac": 0
  +"portal": 0
  +"external_software": 0
  +"company_supplier": "Iezon Solutions"
  +"stars_avg": "5"
}

暂无
暂无

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

相关问题 Laravel Eloquent:如何按产品类别和/或产品品牌过滤产品评论? - Laravel Eloquent: how to filter reviews of products by product category and/or product brand? 如何根据 Laravel 中的类别拉取产品 - How can I pull products according to categories in Laravel 当该类别下的所有产品都缺货时,如何在 woocommerce 类别列表菜单上隐藏产品类别? - How can i hide a product category on woocommerce category list menu when all the products under this category are out of stock? 如何获得 Woocommerce 中每个产品的 5 条最新评论? - How can I get 5 last reviews of each product in Woocommerce? 如何在Laravel中获得对产品的经过验证的评论 - How to get verified reviews of a product in laravel Laravel:如何从我的订单中获得顶级产品? - Laravel: How I can get top products from my orders? 我如何在laravel中获取平均评分的所有数据 - How Can i fetch all data with average rating in laravel 如何在我的 Woocommerce 产品目录中添加六个产品后的 USP 部分 - How Can I add a USP section in between my Woocommerce Product Catlog After Six Products 如果还添加了特定产品,如何从 WooCommerce 的购物车中删除其他产品? - How can I remove other products from my cart in WooCommerce if a specific product is also added? 如何显示已从我的控制器添加的 DB 的最新/最后产品产品 - How can I show the latest/last product products that has been added DB from my controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM