I am new to Laravel. So If I can generate tables using Schema builder and then query data using queries like DB::table('user')->find(1)
, where does Eloquent fit in the whole scene? What purpose does it serve?
我是Laravel的新手。那么,如果我可以使用Schema builder生成表,然后使用DB :: table('user') - > find(1)等查询查询数据,那么Eloquent在整个场景中的位置是什么?它的用途是什么?
1 个解决方案
#1
6
Eloquent is the name given to the ORM (Object-relational mapping) that ships with Laravel. Eloquent allows you to interact with your tables as though they were objects, however Eloquent is unaware of the actual columns you have on your table.
Eloquent是Laravel附带的ORM(对象关系映射)的名称。 Eloquent允许您与表进行交互,就像它们是对象一样,但是Eloquent并不知道您在桌面上的实际列。
Let's consider the simple User
model. We want this model to query records on our users
table.
让我们考虑一下简单的用户模型。我们希望此模型查询用户表上的记录。
class User extends Eloquent {
protected $table = 'users';
}
That there is a very simple model. Now, instead of querying like this.
那是一个非常简单的模型。现在,而不是像这样查询。
$user = DB::table('user')->find(1);
You can query like this.
你可以像这样查询。
$user = User::find(1);
Eloquent itself uses its own query builder but does fall back to the standard query builder. This means it has all the methods on the query builder available to it, and more.
Eloquent本身使用自己的查询构建器,但确实回退到标准查询构建器。这意味着它具有查询构建器上的所有方法,以及更多。
The benefits here are:
这里的好处是:
- You don't have to specify your table name on every call.
- 您不必在每次通话时指定您的表名。
- The code reads a whole lot better, it's syntactical sugar.
- 代码读得更好,这是语法糖。
- You can create complex relationships between tables and use eager loading.
- 您可以在表之间创建复杂关系并使用预先加载。
- You can make use of functionality such as mass assignment protection and setters/getter.
- 您可以使用质量分配保护和设置器/获取器等功能。
- You get all the benefits of using a query builder as well, so you can easily switch between DBMS.
- 您还可以获得使用查询构建器的所有好处,因此您可以轻松地在DBMS之间切换。
I've only touched on Eloquent. There is so much more to it. I suggest you take a look at the following resources.
我只谈过Eloquent。它还有很多东西。我建议你看看以下资源。
- Eloquent Documentation
- 雄辩的文档
- Dayle Rees' Code Happy
- Dayle Rees的代码快乐
- Laravel.io: Fluent and Eloquent
- Laravel.io:流利和雄辩
#1
6
Eloquent is the name given to the ORM (Object-relational mapping) that ships with Laravel. Eloquent allows you to interact with your tables as though they were objects, however Eloquent is unaware of the actual columns you have on your table.
Eloquent是Laravel附带的ORM(对象关系映射)的名称。 Eloquent允许您与表进行交互,就像它们是对象一样,但是Eloquent并不知道您在桌面上的实际列。
Let's consider the simple User
model. We want this model to query records on our users
table.
让我们考虑一下简单的用户模型。我们希望此模型查询用户表上的记录。
class User extends Eloquent {
protected $table = 'users';
}
That there is a very simple model. Now, instead of querying like this.
那是一个非常简单的模型。现在,而不是像这样查询。
$user = DB::table('user')->find(1);
You can query like this.
你可以像这样查询。
$user = User::find(1);
Eloquent itself uses its own query builder but does fall back to the standard query builder. This means it has all the methods on the query builder available to it, and more.
Eloquent本身使用自己的查询构建器,但确实回退到标准查询构建器。这意味着它具有查询构建器上的所有方法,以及更多。
The benefits here are:
这里的好处是:
- You don't have to specify your table name on every call.
- 您不必在每次通话时指定您的表名。
- The code reads a whole lot better, it's syntactical sugar.
- 代码读得更好,这是语法糖。
- You can create complex relationships between tables and use eager loading.
- 您可以在表之间创建复杂关系并使用预先加载。
- You can make use of functionality such as mass assignment protection and setters/getter.
- 您可以使用质量分配保护和设置器/获取器等功能。
- You get all the benefits of using a query builder as well, so you can easily switch between DBMS.
- 您还可以获得使用查询构建器的所有好处,因此您可以轻松地在DBMS之间切换。
I've only touched on Eloquent. There is so much more to it. I suggest you take a look at the following resources.
我只谈过Eloquent。它还有很多东西。我建议你看看以下资源。
- Eloquent Documentation
- 雄辩的文档
- Dayle Rees' Code Happy
- Dayle Rees的代码快乐
- Laravel.io: Fluent and Eloquent
- Laravel.io:流利和雄辩