m.donicova's avatar

Eloquent relationship for order, order_items and products

Hi I have Order, Order_items, Customer and Product in my application

order migration and relatioship

Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('status');
            $table->timestamps();
        });

public function customer()
    {
        return $this->hasOne('App\Customer','id');
    }

customer

Schema::create('customers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->string('street');
            $table->string('city');
            $table->string('psc');
            $table->string('email');
            $table->string('phone');
            $table->timestamps();
        });

order_items

Schema::create('order_items', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('order_id');
            $table->integer('product_id');
            $table->integer('quantity');
            $table->decimal('price',10,2);
            $table->timestamps();
        });

public function product()
    {
        return $this->hasOne('App\Product','id');
    }
}

and Product

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->string('slug')->unique;
            $table->decimal('price',10,2);
            $table->timestamps();
        });

in administartion I have controller for one order detailas, where i need to dispaly customer information, order_items with product name

public function detail($id)
    {
        $order = Order::find($id);

        return view('admin.detail', ['order' => $order]);

    }

and view

@extends('layouts.admin')

@section('content')
    <div class="row">
        {{ $order->customer->firstname }}
    </div>
    <ul>
    @foreach ($order->items as $item)
        <li>{{ $item->product->name }}</li>
    @endforeach
    </ul>
@stop

but this is not function, where is my mistake to work with relationship

0 likes
4 replies
a23mer's avatar

First of all, I think you forgot to add a customer_id column in the Orders table (right now it looks like the primary key is also the customer's id and I assume that's not what you wanted). I would use the following relationship:


public function customer()
{
    return $this->belongsTo(Customer::class, 'customer_id');
}
Then you should be able to use $order->customer (after you modify your relationship by making it point to the customer_id column).

Second, since you are not using the naming convention for the pivot/association table (order_product), you should explicitly declare this relationship in the Order model:


public function products()
{
    return $this->belongsToMany('App\Products', 'order_item');
}

You can read more about pivot tables in this article: http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

shez1983's avatar

i have similar query so posting it here (arrived through google search)

I have similar structure - i am trying to get Products Orders using hasManyThrough but the example of it on laravel docs doesnt fit the db structure I have... any thoughts?

laurent1979's avatar

You do like @a23mer said, but you also need to return the customer in your public function detail. In your table orders, switch user_id for customer_id. Also rewrite your relationships better,

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

{{ $order->customer()->firstname }}

Please or to participate in this conversation.