dimanche 24 janvier 2016

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

I have 2 roles of users called retailer and consumer. I'm using Entrust Role package with the default relationship between role and user in the model. Before I explain next, here is my simple database design with all working example (feel free to ask for anything to include):

=============== MY Database Design with sample ===============

table users

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

table roles

  ______________
 |id |  name    |
 |--------------|
 |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 price: (customer or retailer can claim 1 or more prices):

 _____________________________________
|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 products

 _____________________________
|id      |  user_id| name     |
|------------------------------
|  1     |    1    | Milk     |
|  2     |    2    | Phone    |
|  3     |    1    | computer |
|  4     |    1    | Banana   |
------------------------------

=============== MY Model Relationship ===============

Price model relationship

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

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

Product model relationship

class Product extends Model
{

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

User model relationship //a user can claim 1 or more prices

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

=============== MY Product Controller ===============

This is the tricky part here on how to get the price of all consumers except retailer:

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

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

   $query_consumer =$product->prices()->whereHas('user', function ($q) {
        $q->whereHas('roles', function ($q) {
            $q->where('slug', 'consumer');
        });
    });
    $latest_price_by_consumer= $query_consumer->value('price');

     dd($latest_price_by_consumer); //return 1 row: price 10.00

 } 
}

The query in the controller above return all prices claimed by consumers except retailer. But the problem is, it only return 1 row, the first row which the output is 10.00.

It should output all prices claimed by consumers from the prices table like below:

10.00 6.00 5.00 7.00 8.00

Any idea?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire