This article mainly introduces you to the query builder of the Laravel technique, the data of the overlay chain call, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends below to see together.
Introduction to the Query Builder
Laravel Query Builder provides a convenient, fluent interface for creating and executing database lookup syntax
Use the PDO parameter bindings to protect your application from SQL injection. So the parameters passed in do not require extra escaping special characters
Basically, all database operations can be satisfied and can be performed on all supported database systems
Introduction
Today to introduce you to the Laravel query constructor of a small tip, in the official document example is not mentioned in detail, nor what high-end skills, may be a lot of people in use, do not know the classmate can see.
In the business code often according to different conditions to query, for a simple example, we now have to query the user list, in reverse chronological order, there may be status and type as a qualification.
That's how I wrote it at first.
if ($status && $type) {$users = User::where (' status ', $status)->where (' type ', $type)->latest ()->get () ; } else if ($status) {$users = User::where (' status ', $status)->latest ()->get (); } else if ($type) {$users = use R::where (' status ', $type)->latest ()->get (); } else {$users = User::latest ()->get (); }
This code is really ugly, a lot of public code, such as->latest ()->get (), wrote four times, if the product said today we want to order, then you have to change four places. Although it's quick to change with the editor, it's a simple example.
Look at the following document there is a when method to determine the condition, a bunch of closures is not ideal. I firmly believe that there must be a more elegant wording, so on the StackOverflow search a wave, and sure enough the crooked nuts gave me the answer.
The improved wording:
$query = User::query (); If using DB: $query = db::table (' user '); if ($status) { $query->where (' status ', $status),} if ($type) { $query->where (' type ', $type);} $users = $query->latest ()->get ();
Save the query constructor instance with a variable, and then overlay the constraint on it, the last get collection. The public part is put in the end and end, the structure is clear, is not the superiority of the sentence?
And we can also think of $query as a parameter into a method or function, encapsulating the common logic together to make it easier to invoke multiple places:
function foo ($query) { $query->with ([' Girl ', ' gay ']) ->latest () ->get ();} $query = User::query (); $users = foo ($query);
There is a note to this writing, and once you raise the constraint method in the $query, you will change the query, sometimes we need to clone a query in advance.
For example, we also want to get the users of type 1 and 2
$query _1 = User::query (); $query _2 = Clone $query _1; $users _1 = $query _1->where (' type ', 1)->latest ()->get (); $users _2 = $query _2->where (' type ', 2)->latest ()->get (); Error $users _2 = $query _1->where (' type ', 1)->latest ()->get (); This can be written as type = 1 and $type = 2
Although this example is not written in Laravel's documentation, it is a bit of a mention:
You can start the query using the DB Facade table method. This table method returns a query constructor instance for the query table, allowing you to invoke more constraints at query time and get the final result using the Get method
Off Topic
Before listening to some old-timers said they do not just Baidu programmer, at that time feel really loaded beep, not all search engines, because I did not use Google. Now I also do not want to and only Baidu work together, Baidu is just a search for ads, the search out is what the thing.
Google, StackOverflow is really a good thing, a lot of crooked nuts knowledge, solution Professional, from the computer history to the operating system, databases, a variety of programming languages, to help me de a lot of bugs. It's not good to advertise in Segmentfault.
Summarize
Reference:
How to create multiple WHERE clause query using Laravel eloquent? -StackOverflow
Model::query-laravelapi
Articles you may be interested in:
PHP enables the sharing of Fibonacci sequence codes
PHP based on the binary method to implement array lookup function example explained
A detailed description of classes and objects in PHP