Extending Eloquent Models in Laravel (use different tables) - laravel

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 a refinance_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
}

Related

Accessing involved models in MorphPivot class on boot methods

I have morph many-to-many relation between Tag vs Video and Thread, I'm using a custom model to represent the intermediate table of my relationships, I created a model which extends MorphPivot, So my question is, How to get models on boot method ? For example:
use Illuminate\Database\Eloquent\Relations\MorphPivot;
class TagResource extends MorphPivot
{
protected static function boot()
{
parent::boot();
self::created(function ($model){
dd($model);
});
}
}
I want whenever there is a relationship between my models is created i get them with created event on boot method, But when i dd($model) in the method it just returns the table attributes of TagResource and not the Tag or Video model, Is there a way to get involved models instances ?

What is the best way to instantiate service layer classes?

I've been using laravel for a long time. However, I need better the way I structure my projects.
Sorry for my english, I'm from Brazil.
the structure of my project is: router -> controller -> Service -> Repository -> Model. I place all application logic within the service layer.
My problem is that my services are coupled. And in many situations I'm having circular dependency.
and I use dependency injection to use class instances.
My controllers
class StudentController extends Controller
{
public function __construct(StudentService $studentService){
$this->studentService = $studentService;
}
}
class EnrolmentController extends Controller
{
public function __construct(EnrolmentService $enrolmentService){
$this->enrolmentService = $enrolmentService;
}
}
My services
class StudentService
{
public function __construct(EnrolmentService $nrolmentService){
$this->studentService = $studentService;
}
}
class EnrolmentService
{
public function __construct(Enrolment $studentService){
$this->studentService = $studentService;
}
}
In my example, I need to create an enrollment when registering a student.
You need to change student data when editing an enrollment. The problem is that since one class depends on another, I have a circular dependency problem. I know I should create a third class to try to work around the problem. What I want to know is if this structure of mine is correct. How could I improve this?

Laravel: pluck method on Model instance

I am just learning about Laravel, so excuse me for any newbie questions. In a tutorial I have seen people using pluck directly on Model classes, such as App\MyModel::pluck('id').
My question is where the class gets this method from. It extends the Model class but neither there nor in any trait is it defined. I can only see it defined for collections. I have looked at the source intensely but cannot see it.
Thanks
Zibellon
All of Laravel models they do extend a base class named Model. Example when you define a model the class will look like
class Channel extends Model
{
//stuff
}
Class channel extends base class Model, if you go to Model base class you will find it under Illuminate\Database\Eloquent\Model
That is the abstract base Model class which through some magic methods it recalls the method defined on QueryBuilder under 'Illuminate\Database\Query\Builder' there method pluck is defined

Polymorphic relation in Eloquent ORM (Laravel)

I am trying to achieve an inheritance by using polymorphic relations in Eloquent ORM.
My model schema looks like this:
class Section extends Model {
public function blocks() { // section has many blocks }
}
abstract class Block extends Model {..}
class Exercise extends Block {..}
class Info extends Block {..}
So in my case Section has an array of blocks (and each element could be Exercise or Info).
I tried to tell Eloquent that Section.blocks is an hasMany relation to Block, and Block is morphedTo by it's blockable relation to Exercise or Info, but i failed (and it also doesn't seem like a proper way to do it, because it creates one additional property like $section->blocks[0]->blockable, which should be $section->blocks[0] ).
I also tried to morphTo from Section.blocks right away, but also failed.
Maybe somebody already achived that, and could point me towards right direction.
The question is a little vague but your models should be like below. If there are any difference then add exact errors you get when you fail.
class Section extends Model {
public function blocks() {
return $this->morphTo();
}
}
class Exercise extends Block {
public function sections() {
return $this->morphMany('App\Section', 'blocks');
}
}
class Info extends Block {
public function sections() {
return $this->morphMany('App\Section', 'blocks');
}
}

Implementing class inheritance in codeigniter

I am confused, again, about implementing OOP in CodeIgniter.
By design, I have two classes, namely Customer and Supplier. Both classes extends a super class I call Institution.
It was not a problem for me when I wrote them using only php (without framework).
class Customer extends Institution {
public function __construct() {
parent::__construct();
}
}
class Supplier extends Institution {
public function __construct() {
parent::__construct();
}
}
class Institution extends DBConnection {
public function __construct() {
parent::__construct();
}
}
class DBConnection {
// do CRUD activities here
}
The questions are:
How do I write them using CI?
Is it as Controller or in Model the best way to implement them? What factors should be considered?
A friend suggested a way I thought a bit hacky, model extends model. I try, if possible, to do it in codeigniter-appropriate way.
Any suggestion will be appreciated.
Well, as it is going to be your domain model entities, it should be models. And there is nothing wrong in having another model class as a base class for model class. So, you'll just need to make your Institution (or DBConnection if you prefer to keep it) class extend CI_Model.

Resources