laravel - Eloquent Model has parent model -
i have model called user stuff name, country , relationships.
now want model, e.g. vendor, having same functions , variables user including more stuff
i thought was:
class user extends model implements authenticatablecontract { use authenticatable; softdeletes; protected $dates = ['deleted_at', 'last_login']; protected $fillable = [ 'name', 'password', 'country', ]; protected $hidden = ['password']; public function logs() { return $this->hasmany('app\log'); } }
and vendor model:
class vendor extends user { protected $fillable = [ 'description' ]; public function user() { return $this->belongsto('app\user'); } public function products() { return $this->hasmany('app\product', 'vendor_id'); }
the controller checks role of user , loads user model or vendor model:
if(auth::user()->role > 1) $user = vendor::where('user_id', auth::user()->id)->first(); else $user = auth::user(); return $user->load('logs');
but load call fails vendor. able join fields of user inside vendor need functions of it.
the problem logs function checks field doesn't exists. using functions works:
public function logs() { return $this->hasmany('app\log', 'user_id', get_called_class() !== get_class() ? 'user_id' : 'id'); }
Comments
Post a Comment