【问题标题】:How can I pull out all my products and average the reviews on the product in Laravel?如何在 Laravel 中提取我所有的产品并平均对产品的评论?
【发布时间】:2019-06-11 01:33:39
【问题描述】:
$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();

哪个获得通过该路线的正确产品

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

问题是我有一个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');
    });
}

用户评论存储在一个单独的表中,product_id 与我从数据库中获得的$product 中的id 相关。

我正在尝试根据产品 ID 从所有这些 stars 中获得 AVG() 评级。我试过了

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

但是我收到了这个输出

{#631 ▼
  +"stars_avg": 5.0000
}

而不是我所有的产品信息,额外添加了stars_avg 列。我怎样才能用顶部的这个额外列显示我所有的内部连接?

【问题讨论】:

    标签: php sql laravel laravel-5 pdo


    【解决方案1】:

    我设法通过在选择子查询之前先查询我的数据来解决这个问题。我还在查询中添加了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();
    

    这现在给了我我想要的输出:

    {#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"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      相关资源
      最近更新 更多