Home > Net >  Sql request to Eloquent ORM laravel
Sql request to Eloquent ORM laravel

Time:01-16

I have a small problem for a personal project and I confess I don't really understand how to do it with eloquent from Laravel. I can make the sql query but I have a big problem converting it to eloquent. Here is my problem: I would like to link two tables to an intermediate table.

Here is my intermediate table: all columns :

dish_type_id |  recipe_id

Here is my recipe table: all columns :

recipe_id|  label | image | etc...

Here is my dish_type table: all columns :

dish_type_id |  dish_name

I would like display all the information of recipe and the dish with my intermediate table : That's my sql request :

select * from recipes_dish_type as rcpDish join dish_type dish on dish.DISH_ID = rcpDish.DISH_ID join recipes rcp on rcp.RECIPE_ID = rcpDish.RECIPE_ID;

For the moment, I have a Recipe and DishType model.

Is there any solution with eloquent to have the same thing? I check belongsTo but there is only one way

CodePudding user response:

You can eager load these relationships with with():

$dishTypes = DishType::with(['recipe', 'dish'])->get();

Just double check 'recipe' and 'dish' are correct for your relationships.

  •  Tags:  
  • Related