Asked by: user8286420
Right now I have a model created from this database migration:
Schema::create('likes', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('object_id')->unsigned();
$table->string('object_type');
});
After reading other answers about it I tried playing with morphTo
and morphOne
but I can't seem to figure out how they'd work in this context.
Basically I want to define a function for each type of object that can be referenced by object_type
like so:
public function message()
{
return $this->hasOne('App\Message', 'id', 'object_id');
}
public function status()
{
return $this->hasOne('App\Status', 'id', 'object_id');
}
But with the restriction that the function returns null if object_type
isn't App\Message
or App\Status
respectively.
No comments:
Post a Comment