Search code examples
phplaravelrelationshiplaravel-5.3

Laravel realtionship sometimes not functional


I have Laravel 5. framework for bacis e-shop, i try use relationship I have Order, Order_item, Product, Customer model

Order model has realationship with

class Order extends Model
{
    protected $fillable = ['user_id', 'status'];

    protected $dates = ['created_at'];

    /**
     * Get the items for the order.
     */
    public function items()
    {
        return $this->hasMany('App\Order_item');
    }

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

Order item

class Order_item extends Model
{
    protected $fillable = ['order_id', 'product_id', 'quantity', 'price'];

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

in controller

public function detail($id)
    {
        $order = Order::with('customer','items')->findOrFail($id);

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

    }

and in view I have

<h2>Objednávka č. {{ $order->id }}</h2>
    <h3>Zákazník:</h3>
        {{ $order->customer->firstname }} {{ $order->customer->lastname }}
        <p>{{ $order->customer->email }}</p>
        {{ $order->customer->phone }}
    <h3>Adresa:</h3>
        <p>{{ $order->customer->street }}</p>
        <p>{{ $order->customer->city }}</p>
        <p>{{ $order->customer->psc }}</p>
    <h3>Položky:</h3>
    <table class="table table-bordered table-striped">
        <thead>
            <th>Název</th>
            <th>Počet</th>
            <th>Cena</th>
        </thead>
            <tbody>
            @foreach($order->items  as $item)
                <tr>
                    <th>{{ $item->product->name }}</th>
                    <th>{{ $item->quantity }}</th>
                    <th>{{ $item->price }}</th>
                </tr>
            @endforeach
            </tbody>
        </table>

and now when i test the code for someone order is function but for someone no, the problem is on {{ $item->product->name }}

is my realationship ok? Its a better solution for my e-shop relationship

i post my complete code to github https://github.com/mardon/MaMushashop


Solution

  • Your order item table is a pivot table. There for the relationships to order and product should be declared as belongsToMany.

    • belongsToMany is used in Many To Many.
    • hasMany is used in a One To Many
    • hasOne is used in One To One

    The naming of your table Order_item should be order_product to better reflect what it actually contains.

    class order_product extends Model
    {
        protected $fillable = ['order_id', 'product_id', 'quantity', 'price'];
    
        public function product()
        {
            return $this->belongsToMany('App\Product', 'order_product', 'order_id', 'product_id);
        }
    
        public function order()
        {
            return $this->belongsToMany('App\Order', 'order_product', 'product_id', 'order_id);
        }
    }