Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 Showing a table using a .blade view

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.

like image 523
G.Felicio Avatar asked May 09 '17 11:05

G.Felicio


1 Answers

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>
like image 117
Sapnesh Naik Avatar answered Oct 05 '22 23:10

Sapnesh Naik