Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Eloquent Models in Laravel (use different tables)

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:

  1. Does this strategy make sense?
  2. If it does, how is Eloquent going to handle the data? Will I have a leads table and a refinance_leads table?
  3. 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.

like image 681
jackel414 Avatar asked Mar 30 '16 19:03

jackel414


People also ask

What is eloquent model in Laravel?

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

Is it possible to extend the Laravel query builder?

This is where extending the Laravel query builder comes in handy. (You can name this anything you like to be more specific, like AppBuilder.php or something like that. Just make sure you name the class inside the same as the file name for the namespace to kick in properly ?)

How to create model class using compost in Laravel?

In Laravel framework, we have to create model class for database related operation. So for create model classe using compost, we have go to command prompt and run following command, this command will make model class file with name Country.php under app/Models folder.

What is an eloquent Model Extension?

This technique involves creating new Eloquent models that extend other models. By extending another model, you inherit the full functionality of the parent model, while retaining the ability to add custom methods, scopes, event listeners, etc.


1 Answers

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
}
like image 192
iivannov Avatar answered Oct 19 '22 23:10

iivannov