php - Trouble using Laravel Eloquent here
Get the solution ↓↓↓I'm facing a little trouble returning an array using the Eloquent way in Laravel.
I have two tables:
- menu_items
This table stores name of the menu items and its id. - residents
This table stores resident's (customer) name and id's of their menu selections for lunch and dinner.
Fiddle HERE.
I want to do something like:
$resident = Resident::first();
return $resident->lunch_meal;
// Roast Beef
return $resident->lunch_desert;
// Ice Cream
return $resident->dinner_meal;
// Chicken Pasta
Answer
Solution:
You can try this
$resident = Resident::first();
//return $resident->lunch_meal;
// Roast Beef
return MenuItems::where('id',$resident->lunch_meal)->first()->name??'default';
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.