雄辩的表如何识别? [英] How does eloquent recognize tables?
问题描述
我很好奇,如何雄辩地知道应该通过运行 $ php artisan tinker
在哪个表中保存我们提供的记录。我实际上不记得设置过这样的选项。
I am curious about how eloquent knows in which table it should save the records we give it by running $ php artisan tinker
. I do not actually remember setting so an option.
推荐答案
在 Laravel
使用口才
时,应使用属性 $ table
分配表名,例如:
In Laravel
when using Eloquent
you should assign a table name using the property $table
for example:
protected $table = 'some_thing';
否则,假定表名是模型名的复数形式,在这种情况下, User
模型的表名称应为 users
。以下段落摘自 Laravel
网站:
Otherwise it assumes that the table name is the plural form of the model name and in this case for User
model the table name should be users
. Follwing paragraph is taken from Laravel
website:
表名
请注意,我们并未告知Eloquent我们的Flight
模型使用哪个表。除非明确指定其他名称,否则将使用蛇形类的复数名称作为
表名称。因此,在这种
的情况下,Eloquent将假定飞行模型将记录存储在
航班表中。
Note that we did not tell Eloquent which table to use for our Flight model. The "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Flight model stores records in the flights table.
// You may use this instead:
class Flight extends Model
{
// Explicit table name example
protected $table = 'my_flights';
}
因此,如果在创建/命名数据库时不遵循该约定 Laravel
期望的表,则必须使用受保护的<$ c来告诉 Laravel
模型的表名$ c> $ table 模型中的属性。
So, if you don't follw this convention when creating/naming your database tables that Laravel
expects then you have to tell Laravel
the name of the table for a model using a protected $table
property in your model.
这篇关于雄辩的表如何识别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!