Storing Values in a Polymorphic Pivot Table
Hello. I want to store values in a Polymorphic HasMany relationship.
I have the following three tables:
- Shows
- Attributes
- Attributables
Attributables stores the polymorphic relationship between Shows and Attributes. It looks like this.
- attribute_id
- attributable_id
- attributable_type
- attributable_value
Now, in a regulal polymorphic relationship, we only use the first three columns. I added the attributable_value myself. I would like to store a value here for that specific relationship between Shows and Attributes. How can I cast this onto my models (e.g. Show, Movie, etc.)? How can I save it when creating the relationship?
I started with making a trait to add the attributable behavior to any models requiring it and created an Attribute model. Do I need to expand on the relationship code already written in the framework?
Any help is appreciated.
UPDATE:
I discovered how to save the attributable_value but don't know how to retrieve it without writing additional queries.
$show = App\Models\Show::find(1);
$attribute = new \Waran\Models\Attribute;
$attribute->name = 'Size';
$show->attributes()->save($attribute, ['attributable_value' => 'large']);
Any ideas on how to retrieve the attributable_value when performing the polymorphic query $shows->attributes()?
Please or to participate in this conversation.