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:
meal.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 code: 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(*). this worked for me.
$meals = Meal::select( '*' )->with('category')->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.