In this Article we will see How to Select all records from table with Laravel and Eloquent. We have taken users table and named controller file as userController and model name as User.

Controller File name : UserController.php
Model File name : User.php
View File name : index.blade.php
Table name : users
Columns     : id(primary),name,email, phone
To fetch all records from users table you can write in following 3 ways.
$users = DB::select(‘SELECT * FROM users’);
(or)
$users = DB::table(‘users’)->get();
(or)
$users = User::all(); //Here User is the model

Table:users

CREATE TABLE `users` (
  `id` int(10) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `phone` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `name`, `email`, `phone`) VALUES
(1, 'smita', 'smita@gmail.com', '9861012345'),
(2, 'Smith', 'deep@gmail.com', '9861012346');

You can see we have created a users table with the syntax and inserted 2 records in the table.
Controller: UserController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use App\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {  
        $userobj = new User();
        $users = $userobj->getusers();
        return view(‘index’)->with(‘users’,$users);
    }
}

Model : User.php
<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class User extends Model
{
       protected $table=”users”;
        public function getusers(){
            //$users = DB::select(‘SELECT * FROM users’);
            //$users = DB::table(‘users’)->get();
            $users = User::all();//User is the model here
        return $users;
   }

}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class User extends Model
{
       protected $table=”users”;
        public function getusers(){
            //$users = DB::select(‘SELECT * FROM users’);
            //$users = DB::table(‘users’)->get();
            $users = User::all();//User is the model here
        return $users;
   }

}
View: index.blade.php

 <html>
    <table border="1">
        <tr>
            <td>Sl No</td>
            <td>Name</td>
            <td>Email</td>
            <td>Phone</td>
        </tr>
        @foreach($users as $key=>$user)
        <tr>
            <td>{{$key+1}}</td>
            <td>{{$user->name}}</td>
            <td>{{$user->email}}</td>
            <td>{{$user->phone}}</td>
        </tr>
        @endforeach
    </table>    
</html>

Using the above codes , we can get all records from table in laravel.
Happy Blogging!