· laravel  · 1 min read

Laravel Eloquent Query

Laravel Eloquent functions to query data from database

Laravel Eloquent functions to query data from database

Retrieve single user record from database with/without exception

# Retrieve the user record from database or null
$user = User::find(1);
$user = User::where('id', 1)->first();

# Retrieve the user record from database with exception if not found
$user = User::findOrFail(1);
$user = User::where('id', 1)->firstOrFail();

Retrieve all records from database with sorting

# Retrieve all user records from database
$users = User::all();

# Retrieve the all user records from database order by created_at column descending
$users = User::latest()->get();
$users = User::orderBy('created_at', 'desc')->get();

Retrieve some records from database with limit or pagination

# Retrieve the 10 user records from database as paginated data, the page with change from request
$users = User::paginate(10);
$users = User::offset(request()->get('page', 1))->limit(10)->get();

Retrieve unique user record from database with/without exception


### Logging the executed query of Eloquent

```bash
DB::enableQueryLog();

$users = User::paginate(10);

dd(DB::getQueryLog());
Share:
Back to Blog

Related Posts

View All Posts »

SSH Key

Generate SSH key and use it to connect to remote server