Laravel problem with error to get last 5 rows from database

I dont know where i can have problem. Simple problem: show last five rowns from database in blade view.

database users:

id | name | surrname |
1  | John | Smith    |
2  | Alex | Bimbo    |

cobntroller:

$lastFive = DB::table('users')->orderBy('id', 'desc')->take(5)->get();

blade view:

@foreach($lastFive => $lastFives)
    <tr>
       <td>{{ $lastFives->id }}</td>                        
       <td>{{ $lastFives->name}}</td>
       <td>{{ $lastFives->surrname }}</td>
   </tr>
@endforeach

error:

undefined offset: 1 {"userId":1, "exception":"[object]}

any advice how to figure out what im doing wrong ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

this is incorrect:

@foreach($lastFive => $lastFives)

change it to:

@foreach($lastFive as $lastFives)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x