Hadi.M
Hadi.M

Reputation: 544

laravel 5.6 Eloquent One to Many Relationship on multiple tables

I have some specific database structure as the below diagram

Database Structure

So basically one Part can have multiple specifications but those specifications are not exist in a known table. Specifications can be in any of the rsnf_part_specification , rgr_part_specification or dcd_part_specification tables. I need for find a way to know which table through the part_owner_short_code FK because its the specifications tables prefix rsnf,dcd or rgr.

Is it possible to do this using Laravel 5.6 Eloquent Relationship ?

Upvotes: 2

Views: 2543

Answers (1)

rkj
rkj

Reputation: 8287

You have to modify your table structure like this

owners(id, code, status)
parts(id, owner_id, category_id, name) //should add owner_id as FK 
part_specifications(id, part_id, name, description) //no need to prefix owner code

Owner Model

class Owner extend Model {
    protected $table = 'owners';

    public function parts(){
        return $this->hasMany('App\Part', 'owner_id');
    }

    public function partSpecification(){
        return $this->hasManyThrough('App\PartSpecification', 'App\Part', 'owner_id', 'part_id');
    }

}

Part Model

class Part extend Model {
    protected $table = 'parts';

    public function owner(){
        return $this->belongsTo('App\Owner', 'owner_id');
    }

    public function category(){
        return $this->belongsTo('App\Category', 'category_id'); // Define Category model
    }
}

Part Specification Model

class PartSpecification extend Model {
    protected $table = 'part_specifications';

    public function part(){
        return $this->belongsTo('App\Part', 'part_id');
    }
}

EDIT:

If you want to use the existing specification structure then try this

Owner Model

class Owner extend Model {
    protected $table = 'owners';

    public function parts(){
        return $this->hasMany('App\Part', 'owner_id');
    }
}

Part Model

    class Part extend Model {
        protected $table = 'parts';

        public function owner(){
            return $this->belongsTo('App\Owner', 'owner_id');
        }

        public function category(){
            return $this->belongsTo('App\Category', 'category_id'); // Define Category model
        }

        public function rnsPartSpecification(){
            return $this->hasMany('App\RnsPartSpecification','part_id'); //define RnsPartSpecification model 
        }

        public function rgrPartSpecification(){
            return $this->hasMany('App\RgrPartSpecification','part_id'); //define RgrPartSpecification model 
        }

        public function dcdPartSpecification(){
            return $this->hasMany('App\DcdPartSpecification','part_id'); //define DcdPartSpecification model 
        }
    }

Fetch Data

$parts = Part::with('owner', 'RsnfPartSpecification', 'RgrPartSpecification', 'DcdPartSpecification')->get();

foreach($parts as $part){
    if($part->owner->code == 'rsnf'){
        print_r($part->rnsPartSpecification)
    }else if($part->owner->code == 'rgr'){
        print_r($part->rgrPartSpecification)
    }else{
        print_r($part->dcdPartSpecification)
    }
}

Upvotes: 2

Related Questions