0
votes

I have a users table, a products table, and and an assets table. A user can have many assets, as can a product, meaning both Users, and Products have a 1:n relationship with assets. In Laravel how would show this relationship in Eloquent, is this a Polymorphic relationship, with the struct of the assets table columns being something like,

ID, type, file_path, ownable_id, ownable_type 
2
You are the right way. Just change the ownabe_id, ownable_type to assetable_id and assetable_typeMukti Rani Ghosh

2 Answers

1
votes

I think your assets table columns should be

id, type, file_path, assetable_id, assetable_type

Add this relation to your user and product model

    public function assets()
    {
        return $this->morphMany(Asset::class, 'assetable');
    }

Next, add this relation to your asset model

public function assetable()
{
    return $this->morphTo();
}
0
votes

relation of user model

 public function assets()
    {
        return $this->morphMany(Asset::class, 'assetable');
    }

relation of asset model

public function assetable()
{
    return $this->morphTo();
}