[英]Laravel - eloquent relation
This is meal table:这是餐桌:
Id, Category_Id标识,类别标识
This is categories table:这是类别表:
Id, Slug身份证,蛞蝓
How do I define relationship between those 2 (return array Meal with array category inside to get slug) I tried putting this:我如何定义这 2 之间的关系(返回数组 Meal 与数组类别里面得到蛞蝓)我试着把这个:
meal.php餐.php
function returnCategories()
{
return $this->belongsTo(Category::class);
}
and this doesnt work这不起作用
Edit:编辑:
In Controller file I want to define it like: Meal::with('returnCategories');
在 Controller 文件中,我想将其定义为:
Meal::with('returnCategories');
Controller code: return Meal::select('id')->with('category')->get();
Controller 代码:
return Meal::select('id')->with('category')->get();
try this尝试这个
function returnCategories()
{
return $this->belongsTo(Category::class, 'Category_Id', 'Id');
}
or you can try或者你可以试试
function category()
{
return $this->belongsTo(Category::class, 'Category_Id', 'Id');
}
and try using select(*).并尝试使用 select(*)。 this worked for me.
这对我有用。
$meals = Meal::select( '*' )->with('category')->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.