Skip to content
Advertisement

Eloquent resolve distant relationship

let’s say I’ve this kind of structure.

JavaScript

Is there a way in Laravel Eloquent to load related products given an order? I’ve tried with hasManyThrough(Product::class, Line::class) with no luck, because on products table there’s no reference to the order. Is there a method that can resolve this relationship or should I write a raw query?

Advertisement

Answer

You can create a many-to-many relationship in Order model:

JavaScript

Then you can get products related to an order:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement