Home > Backend Development > PHP Tutorial > What is the difference between Eloquent and DB classes in Laravel5.2?

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

WBOY
Release: 2016-07-06 13:51:44
Original
1268 people have browsed it

Please give an example, thank you!

Reply content:

Please give an example, thank you!

This answer is excerpted from my blog article: "Laravel Learning Notes - Getting Started with Data and Models" (https://www.insp.top/article/learn-laravel-database-and-model-foundation ) has been deleted and edited in response to your question. If you want to know more, just read the original text.

To understand this, let’s first look at things about database components (that is, Eloquent ORM)

The database component is roughly divided into three layers:

  • Database connection layer

  • Query construction layer

  • Application layer

Let’s take a look at what is in each layer and which part of the document they correspond to:

Database connection layer

The database connection layer is the basis on which the entire database component depends. This is self-evident, but this part can actually be seen from the document. It is more based on PDO package, on this basis, provides the following main functions:

  • More intuitive and easy-to-use transactions

  • Read and write separation function

  • Multiple database driver compatibility and switching

  • Database events

This part of the function can be quickly called through Facade (DB class). The documentation has already explained it. Obviously, anyone who has used PDO or MySQLi can quickly get started using parameter binding and SQL preprocessing functions. In order to facilitate event processing, this part encapsulates a method for each of the four operations of adding, deleting, modifying, and checking. In fact, the calling methods are the same (both are methods of the encapsulated PDO and PDOStatement classes), and only the types of results generated are different.

This layer is very low-level. People who need to directly write SQL to operate the database can access it through this layer. In most cases, we will not use it directly (but transactions are very commonly used, we will talk about it in a later article). It should be noted that as the bottom layer, it means that all functions behind the database component are implemented using this layer, so this layer must be understood.

Query construction layer

The

query construction layer consists of 查询构造器 and (语法)生成器, which is a bridge between the application layer and the bottom layer. It provides a smooth access interface, allowing developers to create queries in an elegant form. Although modern frameworks provide such functionality, few like Laravel use many of PHP's excellent features to implement it.

You can query in this way:

<code><?php
$query = DB::table('users')->where('score', '>', 0)
                           ->where(function (Builder $query) {
                               $query->where('code', 'foo')
                                     ->orWhere('name', 'like', 'Anvi%');
                           })->skip(1)
                           ->take(5)
                           ->get();</code>
Copy after login

This will ultimately generate SQL like this:

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

As you can see, the query constructor of the query construction layer provides a very intuitive access method. This method greatly reduces the error probability of constructing SQL statements, and because it is accessed through methods, it is easy for us to access a certain type of query. way to encapsulate (this feature will be mentioned later) to improve development efficiency.

Always remember that the methods such as where or groupBy used are all methods provided by the query constructor. This must be made clear during development, which methods are from which layer and which class Provided by the instance object, this helps avoid unnecessary errors.

The query builder provides an elegant access method, and the final output of the SQL statement is the (grammar) generator. It will generate the corresponding SQL statement based on the currently selected database driver, and finally return it to the query builder for combination It is easy to use binding parameters to call the database connection layer to return query results.

Actually, there are two query constructors in the database component, one is the basic native query constructor, which is provided by the query construction layer, and the other is a re-encapsulated version of Eloqent ORM, Provides more advanced correlation query methods

Application layer

This layer is the part we use for a long time. This layer contains three major components: Eloquent ORM, Migration, Schema.

These three are all implemented based on the query construction layer. Eloquent has the highest appearance rate. Of course, the last two are also very important, namely data migration and structure generator, and data migration components and structure generators are often used as 最佳组件 CP Appear in pairs.

As an application layer, it must be implemented based on lower-level components, which is where you are confused. In fact, just think about it. It's nothing more than ORM encapsulating it again and providing more functions (mainly related queries).

One is to operate as an ORM model (which seems more elegant), and the other is to directly operate and return the data set? ! My own understanding. . .

Eloquent is an ORM, and DB is operated directly using native SQL.
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']);
Regarding the DB facade, please refer to: Laravel database instance tutorial - using the DB facade to operate the database

Eloquent is an ORM. It can help us automate many things, but not all, such as processing direct mapping relationships between tables, simplifying sql, etc.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template