[英]Laravel - eloquent relation
這是餐桌:
標識,類別標識
這是類別表:
身份證,蛞蝓
我如何定義這 2 之間的關系(返回數組 Meal 與數組類別里面得到蛞蝓)我試着把這個:
餐.php
function returnCategories()
{
return $this->belongsTo(Category::class);
}
這不起作用
編輯:
在 Controller 文件中,我想將其定義為: Meal::with('returnCategories');
Controller 代碼: return Meal::select('id')->with('category')->get();
嘗試這個
function returnCategories()
{
return $this->belongsTo(Category::class, 'Category_Id', 'Id');
}
或者你可以試試
function category()
{
return $this->belongsTo(Category::class, 'Category_Id', 'Id');
}
並嘗試使用 select(*)。 這對我有用。
$meals = Meal::select( '*' )->with('category')->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.