Skip to content

WARNING You're browsing the documentation for an old version of Laravel. Consider upgrading your project to Laravel 11.x.

Query Builder


The database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application, and works on all supported database systems.

Note: The Laravel query builder uses PDO parameter binding throughout to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings.


Retrieving All Rows From A Table

$users = DB::table('users')->get();
foreach ($users as $user)

Chunking Results From A Table

DB::table('users')->chunk(100, function($users)
foreach ($users as $user)

You may stop further chunks from being processed by returning false from the Closure:

DB::table('users')->chunk(100, function($users)
return false;

Retrieving A Single Row From A Table

$user = DB::table('users')->where('name', 'John')->first();

Retrieving A Single Column From A Row

$name = DB::table('users')->where('name', 'John')->pluck('name');

Retrieving A List Of Column Values

$roles = DB::table('roles')->lists('title');

This method will return an array of role titles. You may also specify a custom key column for the returned array:

$roles = DB::table('roles')->lists('title', 'name');

Specifying A Select Clause

$users = DB::table('users')->select('name', 'email')->get();
$users = DB::table('users')->distinct()->get();
$users = DB::table('users')->select('name as user_name')->get();

Adding A Select Clause To An Existing Query

$query = DB::table('users')->select('name');
$users = $query->addSelect('age')->get();

Using Where Operators

$users = DB::table('users')->where('votes', '>', 100)->get();

Or Statements

$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')

Using Where Between

$users = DB::table('users')
->whereBetween('votes', [1, 100])->get();

Using Where Not Between

$users = DB::table('users')
->whereNotBetween('votes', [1, 100])->get();

Using Where In With An Array

$users = DB::table('users')
->whereIn('id', [1, 2, 3])->get();
$users = DB::table('users')
->whereNotIn('id', [1, 2, 3])->get();

Using Where Null To Find Records With Unset Values

$users = DB::table('users')

Dynamic Where Clauses

You may even use "dynamic" where statements to fluently build where statements using magic methods:

$admin = DB::table('users')->whereId(1)->first();
$john = DB::table('users')
->whereIdAndEmail(2, '[email protected]')
$jane = DB::table('users')
->whereNameOrAge('Jane', 22)

Order By, Group By, And Having

$users = DB::table('users')
->orderBy('name', 'desc')
->having('count', '>', 100)

Offset & Limit

$users = DB::table('users')->skip(10)->take(5)->get();


The query builder may also be used to write join statements. Take a look at the following examples:

Basic Join Statement

->join('contacts', '', '=', 'contacts.user_id')
->join('orders', '', '=', 'orders.user_id')
->select('', '', 'orders.price')

Left Join Statement

->leftJoin('posts', '', '=', 'posts.user_id')

You may also specify more advanced join clauses:

->join('contacts', function($join)
$join->on('', '=', 'contacts.user_id')->orOn(...);

If you would like to use a "where" style clause on your joins, you may use the where and orWhere methods on a join. Instead of comparing two columns, these methods will compare the column against a value:

->join('contacts', function($join)
$join->on('', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);

Advanced Wheres

Parameter Grouping

Sometimes you may need to create more advanced where clauses such as "where exists" or nested parameter groupings. The Laravel query builder can handle these as well:

->where('name', '=', 'John')
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');

The query above will produce the following SQL:

select * from users where name = 'John' or (votes > 100 and title <> 'Admin')

Exists Statements

->whereRaw('orders.user_id =');

The query above will produce the following SQL:

select * from users
where exists (
select 1 from orders where orders.user_id =


The query builder also provides a variety of aggregate methods, such as count, max, min, avg, and sum.

Using Aggregate Methods

$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');

Raw Expressions

Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings, so be careful not to create any SQL injection points! To create a raw expression, you may use the DB::raw method:

Using A Raw Expression

$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)


Inserting Records Into A Table

['email' => '[email protected]', 'votes' => 0]

Inserting Records Into A Table With An Auto-Incrementing ID

If the table has an auto-incrementing id, use insertGetId to insert a record and retrieve the id:

$id = DB::table('users')->insertGetId(
['email' => '[email protected]', 'votes' => 0]

Note: When using PostgreSQL the insertGetId method expects the auto-incrementing column to be named "id".

Inserting Multiple Records Into A Table

['email' => '[email protected]', 'votes' => 0],
['email' => '[email protected]', 'votes' => 0]


Updating Records In A Table

->where('id', 1)
->update(['votes' => 1]);

Incrementing or decrementing a value of a column

DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes', 5);

You may also specify additional columns to update:

DB::table('users')->increment('votes', 1, ['name' => 'John']);


Deleting Records In A Table

DB::table('users')->where('votes', '<', 100)->delete();

Deleting All Records From A Table


Truncating A Table



The query builder also provides a quick way to "union" two queries together:

$first = DB::table('users')->whereNull('first_name');
$users = DB::table('users')->whereNull('last_name')->union($first)->get();

The unionAll method is also available, and has the same method signature as union.

Pessimistic Locking

The query builder includes a few functions to help you do "pessimistic locking" on your SELECT statements.

To run the SELECT statement with a "shared lock", you may use the sharedLock method on a query:

DB::table('users')->where('votes', '>', 100)->sharedLock()->get();

To "lock for update" on a SELECT statement, you may use the lockForUpdate method on a query:

DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();