Alodon's avatar

Product with Order model relation.

How to do product and order relation. I have tried to do this. Have a look.

Order.php

    public function products(){

        return $this->belongsToMany('App\Product');
    }
Product.php

    public function orders(){

        return $this->belongsTo('App\Order');

    }
AdminOrdersController.php

$orders= Auth::user()->products()->orders()->paginate(10);

But this is creating an error. Do I need any pivot table?

0 likes
8 replies
MichalOravec's avatar

Does your User model has orders relationship?

$orders = Auth::user()->orders()->with('products')->paginate(10);

Alsoin Product model is should be belongsToMany if your relationship is M:N.

public function orders() {
   return $this->belongsToMany('App\Order');
}
Alodon's avatar

But it's getting all products. I am trying to get the ordered products.

judev's avatar

@alodon Fetch the product with the product_id in your table orders. Maybe what do you want to do ? explain it please. Your relation are good and @michaloravec give you the best answer you need to add an where clause if you want to get the ordered products.

MichalOravec's avatar

@alodon I think if user has orders, in that orders should be only products which were ordered. Maybe your DB structure is wrong.

judev's avatar

The structure need to be like this :


------------
|products|

id,
name,
category_id,
principal_image. or nothing if you want to upload multiple image add another table for this,
And Some Other fields you want...

-----------
|  Orders | them table are special you need multiple relation for it for an good data structure

id.
Some fields here....
product_id <-- them is important that references the id on products table, you can do a foreign key for it.
address_id // if the address are in another table.
country_id,

And some fields...

after you've stored them data into your database you can fetch orders table and products table, that are ordered from product_id

Alodon's avatar

OK, So I have a product_id column with a foreign key to the products table. I have tried with pivot table also but it's not working.

japetsky's avatar

Hi Alodon, you should assign a foreign key to one of your tables

public function up()
{
    schema::create('customers', function (Blueprint $table){
        $table->bigIncrements('id');
        $table->unsignedInteger('order_id'); // foreign key
        $table->string('orderName');
        $table->timestamp();
    });
}
nolros's avatar

Although I understand what you attempting to do, you cannot paginate on a collection of a collection. You can get a product and paginate orders on a product, but given the way pagination works it wouldn't make sense. The right way to do it is to get products. List out products and when the user selects a product then get a collection of orders which can be paginated so you can paginate products and then when a product is selected get orders for that product and paginate those orders but you cannot get a collection and for each item get a pagination. When you see what is returned it will make sense.

Please or to participate in this conversation.