Laravel 查询构建器不会 return 所有价格都基于角色 = 客户。只需 return 1 个价格

Laravel query builder doesn't return all prices based on role = customer. Just return 1 price

我正在设计一个应用程序,零售商可以在其中添加具有初始价格的产品(例如存储在产品 table 中),然后客户可以索取从零售商处购买的产品的价格(此信息存储在价格 table 中,如示例所示)。然后,零售商也可以更新/回收价格 table 内的价格。并且客户可以一遍又一遍地收回产品的价格。

所以,我有 2 个用户角色,分别是零售商和客户。我在模型中使用具有角色和用户之间默认关系的委托角色包。在我接下来解释之前,这是我的简单数据库设计和所有工作示例(请随时要求包括任何内容):

=============== 我的数据库设计示例 ===============

table 用户

 __________________________
| id | email   | password |
|-------------------------|
| 1  | a@g.com | 123      |
| 2  | b@g.com | 123      |
| 3    c@g.com | 123      |
| 4    d@g.com | 123      |
 --------------------------

table 角色

  ______________
 |id |  slug    |
 |--------------|
 |1  | customer |
 |2  | retailer |
 ----------------

table role_user

 __________________
 |id_user |  id_role|
 |------------------|
 |  1     |    1    |  -> a@gmail.com is a customer
 |  2     |    2    |  -> b@gmail.com is a retailer
 |  3     |    1    |  -> c@gmail.com is a customer
 |  4     |    1    |  -> d@gmail.com is a customer
  ------------------

table价格: (客户或零售商可以要求 1 个或多个价格):

 _____________________________________
|id|  user_id |  product_id  | price |
|----------------------------|
|1 |    1     |      1       |10.00  | -> price claimed by a customer a@gmail.com on product 1
|2 |    2     |      1       |5.00   | -> price claimed by a retailer b@gmail.com on product 1
|3 |    1     |      1       |6.00   | -> price claimed by a previous customer a@gmail.com on product 1
|4 |    3     |      1       |5.00   | -> price claimed by a customer c@gmail.com on product 1
|5 |    2     |      1       |7.00   | -> price claimed by a previous retailer b@gmail.com on product 1
|6 |    3     |      1       |8.00   | -> price claimed by a customer c@gmail.com on product 1

Table 产品

 _____________________________________
|id      |  user_id| name     | Price
|-------------------------------------
|  1     |    1    | Milk     |  10.00
|  2     |    2    | Phone    |  12.33
|  3     |    1    | computer |  33.44
|  4     |    1    | Banana   |  33.22
--------------------------------------

=============== 我的模型关系 ===============

价格模型关系

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

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

产品型号关系

class Product extends Model
{

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

用户模型关系 //一个用户可以申领1个或多个价格

class User extends Model
{
   public function prices ()
  {
    return $this->hasMany('App\Price');
  }
}

===============我的产品控制器===============

这是关于如何获取除零售商以外的所有客户的价格的棘手部分:

class ProductController extends Controller
{
 public function show($id)
 {
   $product = Product::findOrFail($id); 

   // This query should return all price claimed by customers except retailer. But the problem is, it only return 1 row, the first row which the output is 10.00.

   $query_customer =$product->prices()->whereHas('user', function ($q) {
        $q->whereHas('roles', function ($q) {
            $q->where('slug', 'customer');
        });
    });
    $latest_price_by_customer= $query_customer->value('price');

     dd($latest_price_by_customer); 
     //it just return 1 row: price 10.00

    /* It should return the collection that I can do foreach statement. The output should be like this:

      10.00
      6.00
      5.00
      7.00
      8.00

   */

 } 
}

上面控制器中的查询return除零售商外客户声称的所有价格。但问题是,它只有 return 1 行,输出的第一行是 10.00.

它应该输出客户从价格 table 中声明的所有价格,如下所示:

10.00 6.00 5.00 7.00 8.00

有什么想法吗?

更新:

到目前为止,我更改了我的控制器代码:

   $product = Product::findOrFail($id); 
   $query_customer =$product->prices()->whereHas('user', function ($q) {
        $q->whereHas('roles', function ($q) {
            $q->where('slug', 'customer');
        });
    });
    $latest_price_by_customer= $query_customer->value('price');

     dd($latest_price_by_customer); 

对此:

    $product = Product::with('prices')->findOrFail($id);


    $product_query= $product->prices()->where('product_id', $id) ->whereHas('user', function ($q) {
        $q->whereHas('roles', function ($q) {
            $q->where('slug', 'customer');
        });
    })->select('price')->get();


    dd($product_query); //display collection and return the correct values
   }

我这里有一个小问题:循环遍历集合时

    foreach($product_query->prices as $pr)
    {
       // dd($pr);
       // echo $pr->price . ' ___ ' ;
    }

我在 ProductController.php 第 72 行收到错误 ErrorException:

    Undefined property: Illuminate\Database\Eloquent\Collection::$prices 

但关系是存在的,如图所示。

如果有人在寻找答案,这是 returns 集合而不是 1 行的正确查询:

$product = Product::with('prices')->findOrFail($id);
$product_query= $product->prices()->where('product_id', $id) ->whereHas('user', function ($q) {
        $q->whereHas('roles', function ($q) {
            $q->where('slug', 'customer');
        });
    })->select('price')->get();

    foreach($product_query as $price)
    {
        echo $price->price;
    }