在Laravel中扩展雄辩的模特(使用不同的表) [英] Extending Eloquent Models in Laravel (use different tables)
问题描述
由于潜在客户共享大量信息和功能,但并不是全部,我的想法是创建一个 Lead
类,它扩展了Laravel的模型
类,然后是一个 RefinanceLead
类,它扩展了 Lead
类。
所以我会有:
class Lead extends Model
{
//共享主要东西
}
class RefinanceLead extends Lead
{
//特定于融资线索的东西
我的问题是:
- 这个策略是否有意义?
- 如果有,Eloquent如何处理数据?我会有一个
线索
表和一个refinance_leads
表? - RefinanceLead类的新实例是否会在
线索中使用任何东西?/ code>表?
我没有通过文档回答这个问题,但如果我错过了解释的地方,请让我知道。谢谢。
1。是的,将所有常见功能父母模型。
2。基本上,每个Enloquent模型都将处理从 protected $ table
变量。您可以覆盖父变量以为所有不同的子模型设置单独的表。 Laravel Table Names
例如,如果您在RefinanceLead 实例上使用 getId()
方法,那么它将返回 id refinance_lead 表。如果您在购买实体实例中使用它,它将从购买
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。我不知道是什么你的确切需求,但是一般来说,我会建议引导班抽象,所以你没有关联一个表。使用它只是为了分离常用的功能,关系等...
当然这是在评论中建议的,实现一个界面总是一个好主意。
抽象类Lead扩展模型实现LeadContract
{
//类体
}
I’m building a Laravel application that involves tracking different types of leads. For example, there are Refinance leads and Purchase leads.
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.
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?
I’ve had trouble answering this question via the documentation, but if I missed where this is explained, please let me know. Thanks.
1. Yes, it makes perfect sense to have all the common functionality in a parent model.
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
For example if you use the getId()
method on a RefinanceLead instance it will return the id from refinance_lead table. If you use it on a PurchadeLead instance it will retirn the id from purchade_table
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.
abstract class Lead extends Model implements LeadContract
{
// class body
}
这篇关于在Laravel中扩展雄辩的模特(使用不同的表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!