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

JackD's avatar

updating data form

Good evening,

im getting this error message while trying to update several image and caption in one form

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

here is a part of code of my update method inside controller file

$captions = $request['caption'];
foreach($captions as $caption){
$image->update([
                'caption' => $caption
            ]);
 }

thanks, ci

0 likes
32 replies
JackD's avatar

@SachinAgarwal right now i have an edit form, my question is that why is that the fields im trying to update which is multiple caption field for designated images is not being updated and im getting this error

"preg_replace(): Parameter mismatch, pattern is a string while replacement is an array" 

thanks,

ci

SachinAgarwal's avatar

@Ci var_dump the $captions before foreach() and $caption inside foreach() and tel me what is the output.

Kemito's avatar

You can try to comment out that code if you got same error on dd() and see what happens. Maybe problem is not in this part of code.

SachinAgarwal's avatar

@Kemito mostly the problem is, it is a nested array. I mean even $caption is an array. I had similar prob yesterday, and my mistake was, i was passing array by mistake to the save() method.

JackD's avatar

@Kemito @SachinAgarwal i already tried to dd($captions) but it just give me the same error message

this code is inside my update method

$captions = $request['caption'];
foreach($captions as $caption){
$image->update([
                'caption' => $caption
            ]);
 }
JackD's avatar

@Kemito @SachinAgarwal this is the message im getting after doing dd($captions)

ErrorException in helpers.php line 686:
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
JackD's avatar

@Kemito @SachinAgarwal the error was fixed now after i remove some code above inside method but it is not updating the other caption fields only the last field is updating

JackD's avatar

@kemito @SachinAgarwal here is my method

public function image(Request $request)
    {
    $image = Image::where('userID','=',Auth::user()->id)->firstOrFail();
    $captions = $request['caption'];
    foreach($captions as $caption){
    $image->update([
                'caption' => $caption
            ]);
    }
        return redirect('/');
    }

SachinAgarwal's avatar

@Ci it so obvious that you will get the last field. You are updating same field multiple times, that is, you update method will update same column with caption in array and next time when loop runs, it over writes the previous update. Thus you will get the last caption in array as your updated field.

Kemito's avatar

@Ci are you sure your images table column is called 'userID' instead of 'user_id'?

$captions = $request->get('caption');

Check dump of captions. make sure it returns an array. As well as what ^ he said.

JackD's avatar

@SachinAgarwal @kemito it is user_id sorry

here i can now get some response on dd($captions)

array:2 [▼
  0 => "testing caption 1"
  1 => "testing caption 2"
]
JackD's avatar

@SachinAgarwal @Kemito
i tried this but still the same it still update the only one data with last caption input

public function image(Request $request)
    {
    $image = Image::where('user_id','=',Auth::user()->id)->firstOrFail();
    $captions = $request['caption'];
    foreach($captions as $key=>$caption){
    $image->update([
                'caption' => $captions[$key]
            ]);
    }
        return redirect('/');
    }
SachinAgarwal's avatar

@Ci Read my previous comment. I explained why it is updating only last caption. Its a logical error now. Not a syntax error

JackD's avatar

@SachinAgarwal even though i already have this? im just wondering why it behaves like that while im using the same logic on create form

 foreach($captions as $key=>$caption){
    $image->update([
                'caption' => $captions[$key]
            ]);
    }
Kemito's avatar

@Ci You are updating 1 records value over and over which means that caption value will be last caption in your array

1 like
JackD's avatar

@Kemito @SachinAgarwal thanks :) i get you point but why is that? because of which part of this code?

$captions = $request['caption'];
 foreach($captions as $key=>$caption){
    $image->update([
                'caption' => $captions[$key]
            ]);
    } 
SachinAgarwal's avatar

@Ci this part

foreach($captions as $key=>$caption){
    $image->update([
                'caption' => $captions[$key]
            ]);
    } 

No offence but, your programming skills are not so good. I suggest you learn tracing your code when you get any logical error.

JackD's avatar

@SachinAgarwal yes i know that and i dont claim that im already good in programming that is why im practicing it, though all great programmers started from zero :) im just lost in this part of my code

SachinAgarwal's avatar

@Ci Happens with every starter :) I would suggest you practice tracing your code, you will definitely with the errors by yourself. :)

Next

Please or to participate in this conversation.