Php 拉雷维尔:雄辩地返回一个我可以';不要重复

Php 拉雷维尔:雄辩地返回一个我可以';不要重复,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我有一个mySQL DB模式,如下所示: table RECIPES: int id, PK; int salesman_id; int doctor_id; date order_date; int status; table RECIPE_STATUS: int id, PK; int recipe_status_code; varchar recipe_status_name; 在Laravel中,我有以下php类: AdminRecipesController.php 名称空间

我有一个mySQL DB模式,如下所示:

table RECIPES:

int id, PK;
int salesman_id;
int doctor_id;
date order_date;
int status;

table RECIPE_STATUS:

int id, PK;
int recipe_status_code;
varchar recipe_status_name;
在Laravel中,我有以下php类:

AdminRecipesController.php
名称空间RatCMS\控制器;
使用App\Http\Controllers\Controller;
使用Auth;
使用RatCMS\Models\Recipes;
使用RatCMS\Models\RecipeStatus;
使用\Http\Request;
使用照明\支持\立面\输入;
类AdminRecipesController扩展控制器
{
公共职能指数()
{
$recipes=recipes::with('recipeStatus')->get();
返回视图('admin.recipes.news',compact('recipes');
}
}
Recipes.php

关系应该是
belongsTo()

使用正确的关系,您的代码将向集合添加关系数据,因此您将能够获取状态:

$recipe->recipeStatus->recipe_status_name

这很好,但这并没有回答我的问题好吧,你的问题是为什么关系是空的。使用正确的关系定义,您的代码将返回可迭代的关系数据。无论我使用belongsTo()还是hasOne(),结果都是一样的,我无法在$recipeDid中查看配方_状态您是复制粘贴了我的代码还是刚刚更改了关系?您是否尝试使用我的答案中的代码来显示状态代码名称?因为
recipe\u status
无论如何都不会工作。请显示适当的迁移。谢谢,$recipe->recipeStatus注释是我缺少的!谢谢!尝试将您的状态更改为一个有效的整数,如1,2,3和相应的配方状态代码,并检查结果关于
{{$recipe->recipe\u status->recipe\u status\u name}
<代码>$recipe->recipe\u状态
是一个对象!正在尝试获取非对象的属性..非对象?你看到的
dd($recipe->recipe\u status)是什么
$recipe->recipeStatus->recipe_status_name