model - Table inheritance in Laravel -
as newbie laravel, have problem setting laravel models correctly. in uml diagram, i'm using generalization can "inherit" attributes parent table:
in database, have following.
entity table
------------------- | id | identifier | ------------------- | 1 | azx-c56 | ------------------- | 2 | azx-c06 | ------------------- | 3 | mzx-9f1 | ------------------- | 4 | mzx-x09 | -------------------
worker table
--------------------------------------------- | id | entity_id | firstname | lastname |..| --------------------------------------------- | 1 | 1 | jean | michel |..| --------------------------------------------- | 2 | 2 | jane | doe |..| ---------------------------------------------
machine table
---------------------------------------------- | id | entity_id | type | description | ---------------------------------------------- | 1 | 3 | xu-09-a | lorem ipsum | ---------------------------------------------- | 2 | 4 | xz-24-u | lorem ipsum | ----------------------------------------------
my models respectively:
class entity extends model { protected $fillable = ['identifier']; public function worker(){ return $this->hasone(worker::class); } public function machine(){ return $this->hasone(machine::class); } } class worker extends model { protected $fillable = ['entity_id','firstname','lastname']; public function entity(){ return $this->belongsto(entity::class); } } class machine extends model { protected $fillable = ['entity_id','type','description']; public function entity(){ return $this->belongsto(entity::class); } }
my question is: doing right, or there better way map tables correctly in laravel ?
this not inheritance. association. referencing ids entity
table.
from see id used either worker
or robot
should add constraint xor
.
Comments
Post a Comment