2016-01-24 34 views
3

我正在设计一个零售商可以使用初始价格添加产品的应用程序(例如在产品表中存储商品),然后客户可以向零售商索取产品的价格(此信息以示例中所示的价格表存储)。然后零售商可以更新/回收价格表内的价格。客户可以一遍又一遍地收回产品的价格。Laravel查询生成器不会基于role = customer返回所有价格。只需返回1个价格

所以,我有2个用户称为零售商和客户的角色。我正在使用Entrust Role包与模型中角色和用户之间的默认关系。在我接下来解释之前,这里是我所有工作示例的简单数据库设计(随时要求提供任何内容):

=============== MY Database Design with样品 ===============

用户

__________________________ 
| id | email | password | 
|-------------------------| 
| 1 | [email protected] | 123  | 
| 2 | [email protected] | 123  | 
| 3 [email protected] | 123  | 
| 4 [email protected] | 123  | 
-------------------------- 

角色

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

ROLE_USER

__________________ 
|id_user | id_role| 
|------------------| 
| 1  | 1 | -> [email protected] is a customer 
| 2  | 2 | -> [email protected] is a retailer 
| 3  | 1 | -> [email protected] is a customer 
| 4  | 1 | -> [email protected] is a customer 
    ------------------ 

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

_____________________________________ 
|id| user_id | product_id | price | 
|----------------------------| 
|1 | 1  |  1  |10.00 | -> price claimed by a customer [email protected] on product 1 
|2 | 2  |  1  |5.00 | -> price claimed by a retailer [email protected] on product 1 
|3 | 1  |  1  |6.00 | -> price claimed by a previous customer [email protected] on product 1 
|4 | 3  |  1  |5.00 | -> price claimed by a customer [email protected] on product 1 
|5 | 2  |  1  |7.00 | -> price claimed by a previous retailer [email protected] on product 1 
|6 | 3  |  1  |8.00 | -> price claimed by a customer [email protected] 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('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 

    */ 

} 
} 

查询在控制器上面回报客户声称除了零售商所有的价格。但问题是,它只返回1行,输出为10.00的第一行。

它应该输出由客户从价格表像下面要求的所有价格:

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 

但是存在关系如图所示。

+0

问题是,为什么不同的价格相同的产品以不同的用户? –

+0

表格价格是为了存储特定产品的消费者和零售商声称的价格而设计的。因此,例如,1位消费者可以将产品A声称为10美元,第二天他又可以声称为11美元。零售商也可以这样做。我更新了我的产品表。产品的价格应该留在产品表中。 – MaXi32

+0

你可以转储你的查询,看看真的在发生什么? –

回答

0

如果有人要找的答案,这是返回,而不是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; 
    }