我正在设计一个应用程序,零售商可以在其中添加具有初始价格的产品(例如,存储在产品表中),然后客户可以声明从零售商处购买的产品的价格(此信息存储在价格表中,如例所示)。然后,零售商也可以更新/回收价格表中的价格。客户可以一遍又一遍地收回产品的价格。
因此,我有 2 个用户角色,称为零售商和客户。我正在使用将角色包与模型中的角色和用户之间的默认关系委托给角色包。在我接下来解释之前,这是我的简单数据库设计以及所有工作示例(随意要求包含任何内容):
================带有示例的 MY 数据库设计 ===
============表用户
__________________________
| id | email | password |
|-------------------------|
| 1 | a@g.com | 123 |
| 2 | b@g.com | 123 |
| 3 c@g.com | 123 |
| 4 d@g.com | 123 |
--------------------------
表角色
______________
|id | slug |
|--------------|
|1 | customer |
|2 | retailer |
----------------
表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
------------------
表价格:(客户或零售商可以索赔 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
表产品
_____________________________________
|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('AppProduct');
}
public function user()
{
return $this->belongsTo('AppUser');
}
}
产品型号关系
class Product extends Model
{
public function prices()
{
return $this->hasMany('AppPrice');
}
}
用户模型关系//用户可以声明 1 个或多个价格
class User extends Model
{
public function prices ()
{
return $this->hasMany('AppPrice');
}
}
=============== 我的产品控制器 ===
==============这是有关如何获取除零售商以外的所有客户的价格的棘手部分:
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
*/
}
}
上述控制器中的查询返回除零售商之外的客户声明的所有价格。但问题是,它只返回 1 行,第一行输出为 10.00。
它应该从价格表中输出客户声明的所有价格,如下所示:
10:006.005.007.008.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 . ' ___ ' ;
}
我在产品控制器中收到错误异常错误.php第 72 行:
Undefined property: IlluminateDatabaseEloquentCollection::$prices
但这种关系如图所示存在。
如果有人在寻找答案,这是返回集合而不是 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;
}