What is the difference between eloquent and DB classes in Laravel5.2?

Source: Internet
Author: User
Please illustrate, thank you!

Reply content:

Please illustrate, thank you!

The answer to self-laravel blog post: "Study notes-data and model start" (https://www.insp.top/article/learn-laravel-database-and-model-foundation) For you this problem has been censored, want to know more directly read the original.

To get a look at those things about database components first (that's eloquent ORM)

The database component is roughly three layers:

    • Database connection Layer

    • Query construction Layer

    • Application Layer

Take a look at what each layer has, which part of the document corresponds to:

Database connection Layer

The database connection layer is the basis of the entire database component dependencies, which is self-evident, but this part is actually from the document can be seen, itself is more based on PDO the package, on this basis provides the following several main features:

    • More intuitive and easy-to-use transactions

    • Read/write Separation function

    • Multiple database driver compatibility and switching

    • Database events

This part of the function can be called by facade Quick Call (DB Class), the document has been described, it is clear that as long as the use PDO of the mysqli or can quickly get started using parameter binding and SQL preprocessing capabilities. In order to facilitate event processing, this part of the additions and deletions to change the four operations each encapsulates a method, in fact the call method is consistent (both the encapsulated PDO and the Pdostatement class method), only the resulting type is different.

This layer is very low level, for those who need direct handwritten SQL operations database can access this layer, in most cases we will not use directly (but the transaction is very common, we will be in the following article). It is important to note that as a bottom layer, it means that all the functionality behind the database component is implemented using this layer, so it is necessary to have an understanding of this level.

Query construction Layer

The query construction layer consists of 查询构造器 and consists of the (语法)生成器 application layer and the underlying communication bridge, which provides a smooth access interface, allowing developers to create queries in an elegant form. Although the modern framework provides such functionality, the use of a lot of PHP's excellent features, like Laravel, is rare.

You can query in this way:


  
   where('score', '>', 0)                           ->where(function (Builder $query) {                               $query->where('code', 'foo')                                     ->orWhere('name', 'like', 'Anvi%');                           })->skip(1)                           ->take(5)                           ->get();

It will eventually generate such a SQL:

SELECT * FROM users WHERE score > 0 AND (code = 'foo' OR 'name' LIKE 'Anvi%') OFFSET 1 LIMIT 5;

As you can see, the query constructor of the query construction layer provides a very intuitive way of accessing the error probability of building SQL statements, and because of the method access, it is easy to encapsulate a class of query methods (which is mentioned later) to improve development efficiency.

Always keep in mind that the method used, where or groupBy, is all a method provided by the query constructor, to make it clear in development, which methods are provided by which layer, which class's instance object, which helps to avoid unnecessary errors.

The query constructor provides an elegant access method, and the final output SQL statement is the (grammar) generator, which generates the corresponding SQL statement based on the currently selected database driver, returns to the query constructor, combines the parameters for the binding, and invokes the database connection layer to return the query results.

In fact, the query constructor has two database components, one is the base native query constructor, is provided by the query construction layer, and the other is the Eloqent ORM again encapsulated version, providing a more advanced method of association query

Application Layer

This layer is a part of our long-term use, this layer contains three large components:,, Eloquent ORM Migration Schema .

These three are all based on the query construction layer, eloquent exit rate is the highest, of course, the latter two are also very important, respectively, data migration and structure generator, and data migration components and structure generator as 最佳组件 CP often paired in the appearance.

As an application layer, it must be based on more underlying components, which is where you are wondering. Actually figured out just fine, nothing more than the ORM encapsulates and provides more functionality (mainly associated queries).

One is to operate as an ORM model (it looks more elegant), and one is to directly manipulate the return data set?! My own understanding of ...

Eloquent is an ORM, and DB is directly using native SQL operations.
Eloquent:Post::where('id','<',3)->orderBy('id','desc')->take(1)->get();
Db:DB::insert('insert into users (id, name, email, password) values (?, ?, ? , ? )', [1, 'Laravel','laravel@test.com','123']);
For DB façade, refer to: Laravel database Instance Tutorial--using DB façade to manipulate database

Eloquent is an ORM, and he can help us automate a lot of things, but not all of them, such as dealing with tables directly mapping relationships, simplifying SQL, etc.

  • 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.