Вы должны изменить структуру таблицы следующим образом
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
Модель владельца
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');
}
}
Модель детали
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
}
}
Спецификация детали Модель
class PartSpecification extend Model {
protected $table = 'part_specifications';
public function part(){
return $this->belongsTo('App\Part', 'part_id');
}
}
EDIT:
Если вы хотите использовать существующую структуру спецификации, попробуйте это
Модель владельца
class Owner extend Model {
protected $table = 'owners';
public function parts(){
return $this->hasMany('App\Part', 'owner_id');
}
}
Модель детали
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
}
}
Данные выборки
$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)
}
}