Friday 15 January 2010

laravel - Recommended database approach for website galleries and images -



laravel - Recommended database approach for website galleries and images -

i'm creating schema website have images , object type of post, image may belong to. image database can this:

# 'image' table ---------------- id name url post_id

then, each post, can grab image. but:

two posts can never share image if add together object type of page, i'd have ensure never have same id.

so figure start remove post_id field image more problem worth. best solution kind of manytomany issue? @ moment thinking of having in between table:

# 'gallery' pivot table ------------------------ id object_type [post or page] object_id image_id

now posts , pages can have multiple images. whip through grab associated images object type. right? if makes difference, using laravel framework. i'm building quite big app , conscious if wrong on bite me later.

you still answered question :-) i´d go many many relationships - pivot table needed.

there convention pivot table-name named after models connecting (in alphabetical order). illustration in case pivot-table like:

table: image_post ------------------------ id | image_id | post_id

check documentatin farther information: many-to-many

eager loading interesting part read: eager loading

e.g. querying specific post , images:

$post = post::with( 'images' )->find($id);

database laravel schema

No comments:

Post a Comment