yossi's avatar
Level 2

Naming conventions relationships and routing,

I am sharing what i assume to be the correct way, and some questions. please feel free to add ANY tip or new direction.

Laravel 11

(i know that i can use a connecting table but the images table is used by other parts of the project so i prefer not to.)

tables: articles, images related columns: [articles.image_id, articles.images(a string of iamges.id's] [image.id]

I have a project that shows articles. I want the option to upload an image with each article, and then, allow to upload 5 more images at once.

so i guess it's /articles/new and /articles/addImages (both are route::get's) and corresponding controller actions of route::post type.

i know how to create a model and save it, but how do i do it with related model?

0 likes
4 replies
tykus's avatar

For nested resources, we would typically have a URL format that references the parent record, e.g. POST to articles/{article}/images. This might delegate to a separate Controller, e.g. ArticleImageController:

Route::post('articles/{article}/images', [ArticleImageController::class, 'store']);
 class ArticleImageController
{
    public function store(Request $request, Article $article)
    {
        // store the image(s) associaged with the Article
    }
}

I don't know what you understand by the correct way because nothing about the schema or routing/actions described would be conventional, e.g.

  • why you have image_id and images columns on the articles table?
  • are the images being used by more than one Article?
1 like
yossi's avatar
Level 2

@tykus

thanks for replying.

never gave it a thought about the nested resources & routing.. thanks.

  1. as to the images: yes and no, the table is used for all of the images in the project. they all have much more details. only some are attached to articles...
  2. the image_id if for the "main image", to be used in lists and so.
  3. the images is a list of the images that will be used in an article, by an order which the user sets.

(one thing that i was missing, and now read about it, is the option to save models with with and savemany, this solves some of my issues)

tykus's avatar

@yossi

the table is used for all of the images in the project. they all have much more details. only some are attached to articles

Maybe you need to consider a polymorphic relationship between Image and all other associated Models?

the image_id if for the "main image", to be used in lists...

Having the main image_id on the Article model is perfectly fine; it certainly simplifies the task of retrieving the main image along with the Article.

... the option to save models with with and savemany

I don't like the idea of a comma-separated list if image IDs as its own column on the articles table, and it is useless for eager-loading and/or saveMany. I think you will quickly run into the limitations of this approach. Depending on your needs, a polymorphic or many-to-many relationship would be more conventional.

1 like
martinbean's avatar

the images is a list of the images that will be used in an article, by an order which the user sets.

@yossi This is what many-to-many relations are for. You should not be smashing multiple IDs into a single column. That‘s an inefficient way to store related data, and will also be inefficient when it comes to querying it.

1 like

Please or to participate in this conversation.