Reputation: 721
I have the next tables:
images (id, filename ...)
pages (title, slug ...)
users (username, email ...)
I created an imageables table to keep image relations
image_id / imageable_id / type (Pages / Users etc.)
It works fine if I only need a single image(s) field per model.
But how should I proceed if I need for example header_image and paragraph_image in one page ?
If I want a single header_image / paragraph_image I can keep the field directly in the model (header_image_id, paragraph_image_id) but what if I have a top slider and a gallery for the article.. I will need 2 manyToMany relations.
Is it possible to keep all of them in the same imageables table ? Theoretically I will need another column for unicity (a field name for example.)
Upvotes: 1
Views: 712
Reputation: 1057
I think you are not creating your tables and relationships properly. In your case I would create 2 different tables: pages and images with a pivot table relationship.
Then you can simple use the type column of the Images table to set the place where you want to put that image. You can set type equals to header or paragraph or slider...
$image = new Image;
$image->type= 'header';
$image->filename = 'yourimage.jpg';
...
$image->save();
In your view you can retrieve the image you want for your header, paragraph, slider like this:
$page->images()->where('type', 'header')->first();
Hope it helps!
Upvotes: 1