Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: One to Many to Many, retrieve distinct() values

Laravel 4 Project, using Eloquent ORM.

I have three tables: customers, orders and products (+ 1 pivot table order_product). Customers are linked one-to-many to Orders. Orders are linked many-to-many to Products.

Customers  1-->N  Orders  N<-->N   Products

I would like to have a method on Customer model that retrieves a list of products that customer is buying.

To better understand this, assume products are consumable.

For example Customer #1 can place:

  • Order #1 for Products A, B and C;
  • Order #2 for Products A, C and D;
  • Order #3 for Products C and E;

...and the result I want to retrieve is a Collection with Products A, B, C, D and E.

Models are (pseudo-coded on the fly):

class Product extends Eloquent {

    public function orders()
    {
        return $this->belongsToMany('Order');
    }

}

class Orders extends Eloquent {

    public function customer()
    {
        return $this->belongsTo('Customer', 'customer_id');
    }

    public function products()
    {
        return $this->belongsToMany('Product');
    }

}

class Customers extends Eloquent {

    public function orders()
    {
        return $this->hasMany('Orders', 'customer_id');
    }

    public function products()
    {
        // What to put here ???
    }

}
like image 511
ElementalStorm Avatar asked Apr 08 '14 14:04

ElementalStorm


1 Answers

Thanks to @deczo's answer, I was able to put up a single query method to retrieve items:

public function items()
{
    $query = DB::table('items')->select('items.*')
        ->join('item_order', 'item_order.component_id', '=', 'items.id')
        ->leftJoin('orders', 'item_order.order_id', '=', 'orders.id')
        ->leftJoin('customers', 'customers.id' , '=', 'orders.customer_id')
        ->where('customers.id', $this->id)
        ->distinct()
        ->orderBy('items.id');

    $eloquent = new Illuminate\Database\Eloquent\Builder( $query );
    $eloquent->setModel( new Item );
    return $eloquent->get();
}
like image 124
ElementalStorm Avatar answered Oct 22 '22 01:10

ElementalStorm