Laravel method tutorial for Query Builder overlay chained calls

Source: Internet
Author: User
This article mainly introduces to you about the Laravel technique Query Builder The overlay chain Call of the relevant data, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, hope to help everyone.

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.