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
@Ci
Create images table (images)
id
user_id
path
updated_at
created_at
Image captions table (image_captions)
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.
@Kemito i made the Relationship between images and image_captions as 1:1 because an image can only have one caption, is that right?
@Ci so you dont need a relationship and your table in the original post is ok. What is the problem ?
@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
@Ci you arr saying A user can have many images and you wan to set caption for each image ?
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]">.
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.
@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?
the image is not on a request mode but it is displayed already using tag in my edit form
What ?
@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');
There is a field per caption right ?
@pmall yes there is since they are inside 1 table here is the image structure
id | imagepathname | caption | user_id
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.
@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 sign in or create an account to participate in this conversation.