Gabriel
Gabriel

Reputation: 185

Laravel: how to retrieve all instances of child models which extend from parent model?

We have records of different models but they extend from the same parent model.

I have a page where I would like to display a list of all records of that parent. How would one retrieve all records as the parent model?

example:

class LivingBeing extends Model{
    public function isAlive(){
         return $this->is_alive; //boolean
    }
}

class Human extends LivingBeing{
    use hasSpine; //eloquent handling for spine data
    $table = 'humans';
}

class Cat extends LivingBeing{
    use hasSpine; //eloquent handling for spine data
    $table = 'cats';
}

class Squid extends LivingBeing{
    use hasTentacles; //eloquent handling for tentacle data
    $table = 'squids';
}

I would like to retrieve a morphable eloquent collection of data like:

App\Models\LivingBeing //Human 1
App\Models\LivingBeing //Cat 1
App\Models\LivingBeing //Cat 2
App\Models\LivingBeing //Squid 1

is there a way to query all LivingBeings model similar to.. ?

LivingBeing::all()

I only used biological things as example. our system implements the same as documents instead. :)

Upvotes: 0

Views: 1497

Answers (1)

Abilogos
Abilogos

Reputation: 5055

it depends on how you stored the models in database.

First Approach : Species as a field

if you have one table for living_beings and it has a column name species

you can access all with:

LivingBeing::all();

and in each model you have to implement the scope:

public static function boot()
     {
         parent::boot();
         static::addGlobalScope("specie", function ($query) {
             //specify type in child class
             //$query->where("specie_type", "cat");
             
             //specify in child if you have a species table
             //$query->where("specie_id", 2);
             
             //i think you can do it in parent once but i am not sure
             $query->where("specie", this::class.'');
         });
     }

Second Approach : every Species has a table with living being table

each species has a field name living_being_id

access each species

Cat::all();

or by living being:

    LivingBeing::select("*")::join("cats","cats.living_being_id","living_beings.id")->get();

Third Approach : (not recommended - not data normalized ) every species has a table itself.

to get all species, you have to union query:

Cat::all()->union(Dog::all());

Conclusion

The Best is Approach #1

Upvotes: 1

Related Questions