Sql 请求 Eloquent ORM laravel

Sql request to Eloquent ORM laravel

我在个人项目中遇到了一个小问题,我承认我不太明白如何使用 Laravel 中的 eloquent 来解决这个问题。我可以进行 sql 查询,但在将其转换为 eloquent 时遇到了很大的问题。 这是我的问题:我想 link 两个 table 到中间 table.

这是我的中级 table: 所有列:

dish_type_id |  recipe_id

这是我的食谱table: 所有列:

recipe_id|  label | image | etc...

这是我的 dish_type table: 所有列:

dish_type_id |  dish_name

我想用我的中级 table 显示食谱和菜肴的所有信息: 这是我的 sql 请求:

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;

目前,我有一个 RecipeDishType 模型。

与eloquent是否有相同的解决方案? 我检查 belongsTo 但只有一种方法

您可以使用 with() 预先加载这些关系:

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

只需仔细检查 'recipe' 和 'dish' 是否适合您的关系。