codearts
codearts

Reputation: 2956

Can't display data in view from model in Laravel

In my project I have orders which have many products and customers who have many orders. I am confused because I want to get all orders that a certain customer has and the products of each order. I messed up something somewhere and I am not sure if I set my relationships correctly. Here is my products table:

enter image description here

Here is my customers table:

enter image description here

And here is my orders table:

enter image description here

Here are my models:

Product:

class Product extends Model
{
    public function orders()
    {
         return $this->belongsToMany('App\Order');
    }
}

Order:

class Order extends Model
{
    public function products()
    {
        return $this->hasMany('App\Product', 'id');
    }

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

Customer:

class Customer extends Model
{
    public function orders()
    {
        return $this->hasMany('App\Order', 'id');
    }
}

I get all customers from my database with App\Customer::all() in my CustomersController and pass the data in my customers.blade.php.

<h1>Customers:</h1>
    @foreach($customers as $customer)
        <h3>{{$customer->name}}</h3>
        @foreach($customer->orders as $order)
        <p>Order ID: {{$order->id}}</p>
            @foreach($order->products as $product)
            <p>Product title: {{$product->title}}</p>
            @endforeach
        @endforeach
        <hr>
    @endforeach

Here is the output:

enter image description here

If someone could explain why it doesn't output everything and give some advice if this is the way to go with the relationships, I would be very thankful.

Upvotes: 3

Views: 791

Answers (2)

codearts
codearts

Reputation: 2956

I found a solution by making a pivot table for products and orders called products_orders which holds the product_id and the order_id and making a many to many relationship between Product and Order. That is because an order may have multiple products and products may exist in multiple orders. My pivot table products_orders:

enter image description here

class Product extends Model
{
    public function orders()
    {
         return $this->belongsToMany('App\Order');
    }
}

class Order extends Model
{
    public function products()
    {
        return $this->belongsToMany('App\Product', 'products_orders');
    }

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

I made a one to many relationship (customer_id in orders table) for Customer and Order and now everything works fine.

class Customer extends Model
{
    public function orders()
    {
        return $this->hasMany('App\Order');
    }
}

Upvotes: 0

Joel Hinz
Joel Hinz

Reputation: 25374

Your products should belong to an order, rather than have a many-to-many relationship.

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

Upvotes: 1

Related Questions