Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JackD's avatar

updating multiple field problem

Good morning,

i have this code below as a part of my method for updating the caption of its designated image but the problem is that they belong to same table

image table structure

id | imagepathname | caption | user_id

part of update method code

$captions = $request['caption'];
foreach($captions as $key=>$caps){
    $image = Image::where('user_id', '=', Auth::user()->id)
        ->where()// pseudo code
        ->firstOrFail();
    $image->update([
        'caption' => $captions[$key]
    ]);
}

should i create a caption table and separate the caption field from the image table or is there any better solution for that?

thanks,

ci

0 likes
14 replies
Kemito's avatar

@Ci

Create images table (images)

  • id
  • user_id
  • path
  • updated_at
  • created_at

Image captions table (image_captions)

  • id
  • image_id

Relationship 1:M

Please take a look on your code. Are you sure you want query image (findOrFail) each time you enter cycle? You need to fetch it once.

About code above. Seperate it and store your captions in other table - will make things easier for you.

JackD's avatar

@Kemito i made the Relationship between images and image_captions as 1:1 because an image can only have one caption, is that right?

pmall's avatar

@Ci so you dont need a relationship and your table in the original post is ok. What is the problem ?

JackD's avatar

@pmall because in my current code above it cant find which image it is going to update the caption, since i have the image caption and image field inside 1 table

SachinAgarwal's avatar

@Ci you arr saying A user can have many images and you wan to set caption for each image ?

pmall's avatar

I guess your form have one field for the caption per image so it should be named like this : <input name="images[$image_id][caption]">.

Then in your controller :

$images = $request->get('images');

foreach ($images as $image_id => $image_data) {

    Image::where('id', $image_id)->update($image_data);

}

This way you can update other images values as well as the caption <input name="images[$image_id][another_field]">.

2 likes
Kemito's avatar

Missunderstood then. If image can have just 1 caption then you don`t need sperated table. What you need is @pmall code then - i guess.

1 like
JackD's avatar

@Kemito yeah but then i realized that the image is not on a request mode but it is displayed already using tag in my edit form

the user can only edit the caption when they enter the edit form, but there is a delete button and add new image, it is possible the tag can display an existing data on the database when they are in edit form?

pmall's avatar

the image is not on a request mode but it is displayed already using tag in my edit form

What ?

JackD's avatar

@pmall as i have check the code there was no request for image to submit on the edit form instead it display the existing image using

<img>

tag with the corresponding caption using

<input type="text">

that is why it will give an error message if i placed this code $images = $request->get('images');

pmall's avatar

There is a field per caption right ?

JackD's avatar

@pmall yes there is since they are inside 1 table here is the image structure

id | imagepathname | caption | user_id
pmall's avatar

Reread my post above. if you have a field per image caption so you can name it images[$image_id][caption] and use my code above.

JackD's avatar

@pmall thanks, now this is what im going to use for the caption field in my edit form?

<input name="images[$image_id][caption]">

Please or to participate in this conversation.