Eloquent many to many relationship access table column value from pivot table

Amir H

I have two models:

class Order extends Eloquent 
{
    public function User()
    {
        return $this->belongsTo('User');
    }

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

and the second one is:

class Product extends Eloquent 
{
    public function Order()
    {
        return $this->belongsToMany('Order');
    }
}

My question is how can i access a value of second table column using pivot table:

product table:

id
title
image

order table:

id
status

pivot table(order_product):

id
product_id
order_id

I need to access title column of products from orders. for example if a user orders many products in one order I can fetch all product title and show theme. I don't like use join , instead I like to use Laravel functionality itself.

Amir H

I found the answer:

$orders = Order::orderBy('created_at', 'DESC')->paginate($page_number);

foreach($orders as $item){
  foreach($item->product as $item){
     {{$item->title}}<br>
  }
}

I can access products from order.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Laravel L5.5 No access to pivot table in "Many to many" relationship

From Dev

Laravel - Order by pivot value in Many to Many table relationship

From Dev

Eloquent - Many to many where not found in the pivot table

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Many-to-Many relationship in Access from a single table

From Dev

Pivot Table with many to many table

From Dev

Eloquent Many to Many with Pivot table to refactor the returned results to a specific array

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

From Dev

Unable to sync conditional pivot table many to many relationship laravel

From Dev

Laravel 4.2 Many-to-many relationship : Can't read from pivot table

From Dev

How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

From Dev

Recursive relationship on a many to many table

From Dev

SQL many to many relationship table

From Dev

Many to many relationship in the same table?

From Dev

Junction table/many to many relationship

From Dev

SQL query Pivot Many to Many to Many Table

From Dev

Eloquent relationship in custom pivot table

From Dev

Update a column that exist in the many to many relationship table in laravel 5.2

From Dev

Sequelize many-to-many relationship table gets unneeded extra column

From Dev

Many-to-many relationship between same table with extra column in EF

From Dev

One to many relationship table

From Dev

Fetching one to many relationship from same table

From Dev

How to GROUP and SUM a pivot table column in Eloquent relationship?

From Dev

Query for retrieving data from a bridging table of many to many relationship

From Dev

Select many intervals from a single table based on column value

Related Related

  1. 1

    Laravel Eloquent Many to Many relationship using a pivot table

  2. 2

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  3. 3

    Laravel L5.5 No access to pivot table in "Many to many" relationship

  4. 4

    Laravel - Order by pivot value in Many to Many table relationship

  5. 5

    Eloquent - Many to many where not found in the pivot table

  6. 6

    Creating one to many relationship with pivot table of many to many relationship

  7. 7

    Creating one to many relationship with pivot table of many to many relationship

  8. 8

    Many-to-Many relationship in Access from a single table

  9. 9

    Pivot Table with many to many table

  10. 10

    Eloquent Many to Many with Pivot table to refactor the returned results to a specific array

  11. 11

    How to query a pivot table data in MySQL (many to many relationship)

  12. 12

    Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

  13. 13

    Unable to sync conditional pivot table many to many relationship laravel

  14. 14

    Laravel 4.2 Many-to-many relationship : Can't read from pivot table

  15. 15

    How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

  16. 16

    Recursive relationship on a many to many table

  17. 17

    SQL many to many relationship table

  18. 18

    Many to many relationship in the same table?

  19. 19

    Junction table/many to many relationship

  20. 20

    SQL query Pivot Many to Many to Many Table

  21. 21

    Eloquent relationship in custom pivot table

  22. 22

    Update a column that exist in the many to many relationship table in laravel 5.2

  23. 23

    Sequelize many-to-many relationship table gets unneeded extra column

  24. 24

    Many-to-many relationship between same table with extra column in EF

  25. 25

    One to many relationship table

  26. 26

    Fetching one to many relationship from same table

  27. 27

    How to GROUP and SUM a pivot table column in Eloquent relationship?

  28. 28

    Query for retrieving data from a bridging table of many to many relationship

  29. 29

    Select many intervals from a single table based on column value

HotTag

Archive