Laravel 5.2 Showing a table using a .blade view
In your controller function do this right after you store a new record to table.
assuming your model name is User
fetch all user records from User model,
$users = App\User::all();
pass this users variable to view(assuming your view.blade.php
is in resources/views
folder)
return View::make('view', compact('users'));
and then in your view.blade.php you can do something like this to display all user records.
<table>
<thead>
<tr>
<th> id</th>
<th> name</th>
<th> last name </th>
<th> email </th>
<th> phone</th>
<th> adddress </th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td> {{$user->id}} </td>
<td> {{$user->name}} </td>
<td> {{$user->last_name}} </td>
<td> {{$user->email}} </td>
<td> {{$user->phone}} </td>
<td> {{$user->address}} </td>
</tr>
@endforeach
</tbody>
</table>
G.Felicio
Being interested in games my whole life opened up my perceptions and my curiosity for creating. Starting my dev life creating games expanded my vision on what it means to code.
Updated on April 06, 2020Comments
-
G.Felicio about 4 years
I have my DB, already migrated everything, my code can record new entries in my table when required, and so on.
I have a table with the contents of: 'id', 'name', 'lastname', 'email', 'phone', 'address'.
What I want is, when you hit the submit button, you'll be redirected to a new 'view.blade.php' where you can see all of the DB's entries, including the most recent. BUT I don't know how to develop this code to make it work.
I need help with that, please. Thank you.
-
G.Felicio almost 7 yearsIT WORKS! Thank you!
-
Mihir Bhende almost 7 yearsYou need to always check if count of users is greater than 0 before starting foereach!
-
G.Felicio almost 7 yearsYes, Mihir. Since my table already had some info (I was viewing it in a separate program, DBeaver), I overlooked it, but that's a good input. Thank you.