Laravel : Retrieving All Records From Database Table

In this tutorial learn How to retrieving all records from database and display records with foreach loop in laravel.

You may use the table method on the DB facade to begin a query. The table method returns a fluent query builder instance for the given table.
1. Create UserController to apps/controllers
 <?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show a list of all of the application's users.
     *
     * @return Response
     */
    public function index()
    {
        $users = DB::table('users')->get();

        return view('user', ['users' => $users]);
    }
}
2. Create  a user in views/ user.blade.php

The get method returns an IIuminate\Support\Collection containing the results where each result is an instance of the PHP stdClass object. You may access each column's value by accessing the column as a property of the object: use the foreach to display the records  
@foreach ($users as $user) 
     {{$user->name}}
@endforeach

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...