To retrieve all records from a database table using Eloquent in Laravel, you can follow the steps below:
php artisan make:model
command or manually in the app\Models
directory. For example, to fetch records from a table named users
, you can create a User
model:php artisan make:model User
app\Models\User.php
), import the Illuminate\Database\Eloquent\Model
class and specify the table name by setting the protected $table
property:namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'users';
}
all()
method on the model:use App\Models\User;
...
$users = User::all();
The all()
method will return a collection of all the records in the users
table. You can then loop through the collection or perform any other operations you require.
Note: Don't forget to import the User
model at the top of your file (use App\Models\User;
).