query builder
- Introduction
- Get results
- Query
- Native expression
- Joins
- Unions
- Where statement
- ##Ordering, Grouping, Limit, & Offset
- Conditional Statement
- Insert
- Update
- Update JSONDelete
- Pessimistic Lock
chunkById method. This method will automatically paginate the results based on the record's primary key:
DB::table('users')->where('active', false) ->chunkById(100, function ($users) { foreach ($users as $user) { DB::table('users') ->where('id', $user->id) ->update(['active' => true]); } });
{Tip} When updating or deleting records inside a block's callback, any changes to the primary key or foreign key may affect the block query. . This may result in records not being included in chunked results.AggregationThe query builder also provides various aggregation methods, such as
count ,
max,
min,
avg, and
sum. You can call any method after constructing the query:
$users = DB::table('users')->count(); $price = DB::table('orders')->max('price');Of course, you can also combine these aggregation methods with other query statements:
$price = DB::table('orders') ->where('finalized', 1) ->avg('price');Determine whether the record ExistsIn addition to using the
count method to determine whether the result of the query condition exists, you can also use the
exists and
doesntExist methods:
return DB::table('orders')->where('finalized', 1)->exists(); return DB::table('orders')->where('finalized', 1)->doesntExist();
Of course you may not It is not always desirable to obtain all columns from a database table. Using the
select method, you can customize a select
query statement to query the specified field: The $users = DB::table('users')->select('name', 'email as user_email')->get();
method will force the query to return The results are not duplicated:
If you already have a query builder instance and want to add a field to the existing query statement, then you can use the $users = DB::table('users')->distinct()->get();
method: $query = DB::table('users')->select('name');
$users = $query->addSelect('age')->get();
Sometimes you may need to use native expressions in queries. You can create a raw expression using
DB::raw: $users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
You can use the following method instead
DB::raw , inserting native expressions into various parts of the query.
selectRaw
selectRaw
method can be used instead of select(DB::raw(...))
. The second parameter of this method is optional, and the value is an array of bound parameters:
$orders = DB::table('orders') ->selectRaw('price * ? as price_with_tax', [1.0825]) ->get();
whereRaw / orWhereRaw
## The #whereRaw and
orWhereRaw methods inject the native
where into your query. The second parameter of these two methods is still optional, and the value is still an array of bound parameters:
$orders = DB::table('orders') ->whereRaw('price > IF(state = "TX", ?, 100)', [200]) ->get();##havingRaw / orHavingRaw
# The ##havingRaw
and orHavingRaw methods can be used to set a raw string to the value of the
having statement:
$orders = DB::table('orders') ->select('department', DB::raw('SUM(price) as total_sales')) ->groupBy('department') ->havingRaw('SUM(price) > ?', [2500]) ->get();
## The #orderByRaworderByRaw
method can be used to set a raw string to the value of the
order by clause: $orders = DB::table('orders')
->orderByRaw('updated_at - created_at DESC')
->get();
Inner Join Clause
method. To perform basic
"inner joining", you can use thejoin method on the query builder instance. The first parameter passed to the
join
method is the name of the table you want to join, while the other parameters use the field constraints that specify the join. You can also join multiple data tables in a single query: $users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
Left Join statement
or
rightJoin method. These two methods have the same usage as the join
method: $users = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get(); $users = DB::table('users')
->rightJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
Cross Join statement
method as you want The name of the table to be connected is called "cross connection". A cross join generates a Cartesian product between the first table and the joined table:
$users = DB::table('sizes') ->crossJoin('colours') ->get();
Advanced Join Statement
as the second parameter of the
join method. This Closure
receives a JoinClause
object, thereby specifying the constraints specified in the
join
statement:
If you want to To use "where" style statements on connections, you can use the DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
where
and
methods on connections. These methods compare columns to values instead of columns to columns: DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
Subjoin query
The ,
leftJoinSub and rightJoinSub
methods join a query as a subquery. Each of their methods receives three parameters: a subquery, a table alias, and a closure that defines the associated fields: $latestPosts = DB::table('posts')
->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)
->groupBy('user_id');$users = DB::table('users')
->joinSub($latestPosts, 'latest_posts', function($join) {
$join->on('users.id', '=', 'latest_posts.user_id');
})
->get();
whereNotNull The method verifies that the specified field must not be
NULL:
$users = DB::table('users') ->whereNotNull('updated_at') ->get();
whereDate / whereMonth / whereDay / whereYear / whereTime
whereDate method is used to compare the field value with the given date:
$users = DB::table('users') ->whereDate('created_at', '2018-09-08') ->get();
whereMonth method is used to compare the field value with The specified month of the year:
$users = DB::table('users') ->whereMonth('created_at', '9') ->get();
whereDay method is used to compare the field value with the specified day of the month:
$users = DB::table('users') ->whereDay('created_at', '8') ->get();
whereYear method Used to compare the field value with the specified year:
$users = DB::table('users') ->whereYear('created_at', '2018') ->get();
whereTime The method is used to compare the field value with the specified time (hours, minutes and seconds):
$users = DB::table('users') ->whereTime('created_at', '=', '11:20:45') ->get();
whereColumn
whereColumn The method is used to compare whether the values of two fields are equal:
$users = DB::table('users') ->whereColumn('first_name', 'last_name') ->get();You can also pass in a comparison operator:
$users = DB::table('users') ->whereColumn('updated_at', '>', 'created_at') ->get();
whereColumn You can also pass arrays linked using the
and operator:
$users = DB::table('users') ->whereColumn([ ['first_name', '=', 'last_name'], ['updated_at', '>', 'created_at'] ])->get();Parameter GroupingSometimes you need to create more advanced where clauses, such as "where exists" or nested parameter grouping. Laravel's query builder can handle this as well. Next, let's look at an example of grouping constraints within parentheses:
DB::table('users') ->where('name', '=', 'John') ->where(function ($query) { $query->where('votes', '>', 100) ->orWhere('title', '=', 'Admin'); }) ->get();You can see that a query construct is built by a
Closure write
where method Container to constrain a group. This
Closure receives a query instance that you can use to set the constraints that should be included. The above example will generate the following SQL:
select * from users where name = 'John' and (votes > 100 or title = 'Admin')
{Tip} You should call this group usingWhere Exists statementorWhere
to avoid unexpected application global effects.
whereExists method allows you to use the
where exists SQL statement . The
whereExists method accepts a
Closure parameter. The
whereExists method accepts a Closure parameter. This closure obtains a query builder instance allowing you to define the
exists Query in the sentence:
DB::table('users') ->whereExists(function ($query) { $query->select(DB::raw(1)) ->from('orders') ->whereRaw('orders.user_id = users.id'); }) ->get();The above query will generate the following SQL statement:
select * from users where exists ( select 1 from orders where orders.user_id = users.id)
JSON Where statement
Laravel also supports querying JSON type fields (only on databases that support JSON type). Currently, this feature only supports MySQL 5.7, PostgreSQL, SQL Server 2016, and SQLite 3.9.0 (with the JSON1 extension). Use the ->
operator to query JSON data:
$users = DB::table('users') ->where('options->language', 'en') ->get();$users = DB::table('users') ->where('preferences->dining->meal', 'salad') ->get();
You can also use whereJsonContains
to query JSON arrays:
$users = DB::table('users') ->whereJsonContains('options->languages', 'en') ->get();
MySQL and PostgreSQL whereJsonContains
Can support multiple values:
$users = DB::table('users') ->whereJsonContains('options->languages', ['en', 'de']) ->get();
You can use whereJsonLength
to query the length of the JSON array:
$users = DB::table('users') ->whereJsonLength('options->languages', 0) ->get();$users = DB::table('users') ->whereJsonLength('options->languages', '>', 1) ->get();
Ordering, Grouping, Limit, & Offset
orderBy
orderBy
method allows you to pass Sorts the result set by a given field. The first parameter of orderBy
should be the field you want to sort, and the second parameter controls the direction of sorting, which can be asc
or desc
:
$users = DB::table('users') ->orderBy('name', 'desc') ->get();
latest / oldest
latest
and oldest
methods allow you to sort by date easily. By default it uses the created_at
column as the sort by. Of course, you can also pass custom column names:
$user = DB::table('users') ->latest() ->first();
inRandomOrder
inRandomOrder
method is used to randomly order the results. For example, you can use this method to find a random user.
$randomUser = DB::table('users') ->inRandomOrder() ->first();
groupBy / having
groupBy
and having
methods can group the results. The usage of the having
method is very similar to the where
method:
$users = DB::table('users') ->groupBy('account_id') ->having('account_id', '>', 100) ->get();
You can pass multiple parameters to the groupBy
method:
$users = DB::table('users') ->groupBy('first_name', 'status') ->having('account_id', '>', 100) ->get();
For more advanced having
syntax, see the havingRaw
method.
skip / take
To limit the number of results returned, or to skip a specified number of results, you can use skip
and take
method:
$users = DB::table('users')->skip(10)->take(5)->get();
Or you can also use limit
and offset
method:
$users = DB::table('users') ->offset(10) ->limit(5) ->get();
Conditional Statements
Sometimes you may want a clause to only execute a query if a certain condition is true. For example, you might only apply a where
statement if a given value exists in the request. You can do this by using the when
method:
$role = $request->input('role');$users = DB::table('users') ->when($role, function ($query, $role) { return $query->where('role_id', $role); }) ->get();
when
The method will only execute the given closure when the first parameter is true
Bag. If the first parameter is false
, then this closure will not be executed
You can pass another closure as the third parameter of the when
method . This closure will be executed if the first parameter is false
. To illustrate how to use this feature, let's configure the default ordering for a query:
$sortBy = null;$users = DB::table('users') ->when($sortBy, function ($query, $sortBy) { return $query->orderBy($sortBy); }, function ($query) { return $query->orderBy('name'); }) ->get();
INSERT
Query Construction The processor also provides the insert
method for inserting records into the database. The insert
method receives field names and field values in the form of arrays for insertion operations:
DB::table('users')->insert( ['email' => 'john@example.com', 'votes' => 0]);
You can even pass an array to the insert
method to insert multiple records into the table Medium
DB::table('users')->insert([ ['email' => 'taylor@example.com', 'votes' => 0], ['email' => 'dayle@example.com', 'votes' => 0] ]);
Auto-increment ID
If the data table has an auto-increment ID, use the insertGetId
method to insert the record and return the ID value
$id = DB::table('users')->insertGetId( ['email' => 'john@example.com', 'votes' => 0] );
{Note} When using PostgreSQL, the
insertGetId
method will default toid
as the name of the auto-increment field. If you want to get the ID from another "sequence", you can pass the field name as the second parameter to theinsertGetId
method.
Update
Of course, in addition to inserting records into the database, the query builder can also pass update
Method updates existing records. The update
method is the same as the insert
method, accepting an array containing the fields and values to be updated. You can constrain the update
query through the where
clause:
DB::table('users') ->where('id', 1) ->update(['votes' => 1]);
Update or add
Sometimes you may Want to update an existing record in the database, or create a matching record if one does not exist. In this case, the updateOrInsert
method can be used. updateOrInsert
The method accepts two parameters: a condition array for finding records, and an array of key-value pairs containing the record to be updated.
updateOrInsert
The method will first try to find a matching database record using the key and value pair of the first parameter. If the record exists, use the value in the second parameter to update the record. If the record is not found, a new record will be inserted and the updated data is a collection of two arrays:
DB::table('users') ->updateOrInsert( ['email' => 'john@example.com', 'name' => 'John'], ['votes' => '2'] );
Update JSON field
When updating a JSON field, you can use the ->
syntax to access the corresponding value in the JSON object. This operation only supports MySQL 5.7:
DB::table('users') ->where('id', 1) ->update(['options->enabled' => true]);
Increment and Decrement
The query constructor also provides convenient methods for incrementing or decrementing a given field. This method provides a more expressive and concise interface than manually writing update
statements.
Both methods receive at least one parameter: the column that needs to be modified. The second parameter is optional and controls the amount by which the column is incremented or decremented:
DB::table('users')->increment('votes'); DB::table('users')->increment('votes', 5); DB::table('users')->decrement('votes'); DB::table('users')->decrement('votes', 5);
You can also specify the fields to be updated during the operation:
DB::table('users')->increment('votes', 1, ['name' => 'John']);
Delete
The query builder can also delete records from the table using the delete
method. Before using delete
, you can add a where
clause to constrain delete
Syntax:
DB::table('users')->delete(); DB::table('users')->where('votes', '>', 100)->delete();
If you need to clear the table, you can use truncate
method, which will delete all rows and reset the auto-incremented ID to zero:
DB::table('users')->truncate();##Pessimistic lock The query builder also contains some functions that can help you implement "pessimistic locking" on the
select syntax. If you want to implement a "shared lock" in a query, you can use the
sharedLock method. Shared locks prevent selected data columns from being tampered with until the transaction is committed:
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();Alternatively, you can use the
lockForUpdate method. Using the "update" lock can prevent rows from being modified or selected by other shared locks:
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();This article was first published on the