Skip to content
Advertisement

fetch join table data using eloquent laravel

I am new to laravel & want to implement eloquent relationship.

Let me explain.

Consider I have 2 tables

products

JavaScript

brands

JavaScript

Each product will have one brand Id.But in Brands table, there is no product id. One brand_id can be in multiple product rows, and one product has one brand_id only. I want to select some col from products table plus brand_name with respect to brand_id of products table using Model.SO in Product model I wrote:

JavaScript

and in Brand model I write:

JavaScript

Now I want the result:

JavaScript

How can I fetch those data in controller using eloquent relation? Also, the way I wrote Model relationship, Is it ok??

Advertisement

Answer

Your Product Model relation will be below

JavaScript

Now in controller you can add query as below.

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