在 Laravel 中扩展 Eloquent 模型(使用不同的表)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36318164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extending Eloquent Models in Laravel (use different tables)
提问by Hymanel414
I'm building a Laravel application that involves tracking different types of leads. For example, there are Refinance leadsand Purchase leads.
我正在构建一个涉及跟踪不同类型潜在客户的 Laravel 应用程序。例如,有再融资线索和采购线索。
Since the leads share a lot of information and functionality, but not all, my thinking was to create a Lead
class, which extends Laravel's Model
class, and then a RefinanceLead
class, which extends the Lead
class.
由于leads共享了很多信息和功能,但不是全部,我的想法是创建一个Lead
类,它扩展了Laravel的Model
类,然后是一个RefinanceLead
扩展Lead
类的类。
So I'd have:
所以我有:
class Lead extends Model
{
// shared lead stuff
}
class RefinanceLead extends Lead
{
// stuff specific to refinance leads
}
My questions are:
我的问题是:
- Does this strategy make sense?
- If it does, how is Eloquent going to handle the data? Will I have a
leads
table and arefinance_leads
table? - Will a new instance of the RefinanceLead class utilize anything in the
leads
table?
- 这个策略有意义吗?
- 如果是这样,Eloquent 将如何处理数据?我会有一张
leads
桌子和一张refinance_leads
桌子吗? - RefinanceLead 类的新实例是否会使用
leads
表中的任何内容?
I've had trouble answering this question via the documentation, but if I missed where this is explained, please let me know. Thanks.
我无法通过文档回答这个问题,但如果我错过了解释的地方,请告诉我。谢谢。
回答by iivannov
1.Yes, it makes perfect sense to have all the common functionality in a parent model.
1.是的,在父模型中拥有所有通用功能是非常有意义的。
2.Basically each Eloquent model will handle the data from its own table defined in the protected $table
variable. You can override the parent variable to set a separate table for all the different child models. Laravel Table Names
2.基本上每个 Eloquent 模型都会处理protected $table
变量中定义的自己表中的数据。您可以覆盖父变量为所有不同的子模型设置单独的表。Laravel 表名
For example if you use the getId()
method on a RefinanceLeadinstance it will return the idfrom refinance_leadtable. If you use it on a PurchadeLeadinstance it will retirn the idfrom purchade_table
例如,如果您getId()
在RefinanceLead实例上使用该方法,它将从refinance_lead表中返回id。如果您在PurchadeLead实例上使用它,它将从purchade_table 中回收id
class Lead extends Model
{
public function getId() {
return $this->id;
}
}
class RefinanceLead extends Lead
{
protected $table = 'refinance_leads';
}
class PurchaseLead extends Lead
{
protected $table = 'purchase_leads';
}
3.I don't know what are your exact needs, but in general I'll suggest making the Lead class abstract and so you don't associate a table for it. Use it only to separate common functionality, relations, etc... Of course as it was suggested in the comments, implementing an interface is always a good idea.
3.我不知道您的确切需求是什么,但总的来说,我建议将 Lead 类设为抽象类,这样您就不必为它关联表。仅使用它来分隔公共功能、关系等......当然,正如评论中所建议的那样,实现接口始终是一个好主意。
abstract class Lead extends Model implements LeadContract
{
// class body
}