@SachinAgarwal bro any idea on this?
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
@Ci what exactly is the question? :P
@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
@Ci var_dump the $captions before foreach() and $caption inside foreach() and tel me what is the output.
@SachinAgarwal it gives me the same error message after i var_dump the captions
dd($captions);
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.
@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.
@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
]);
}
@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
@Ci try dd($request);
@Ci can you show the full method in which you are doing this?
@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
@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('/');
}
@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.
@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.
@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"
]
@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('/');
}
@Ci Read my previous comment. I explained why it is updating only last caption. Its a logical error now. Not a syntax error
@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]
]);
}
@Ci it is because of that only
@SachinAgarwal sorry i dont get what you mean in your last comment :)
@Ci You are updating 1 records value over and over which means that caption value will be last caption in your array
@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]
]);
}
@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.
@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
@Ci Happens with every starter :) I would suggest you practice tracing your code, you will definitely with the errors by yourself. :)
@SachinAgarwal yup same same :)
Please or to participate in this conversation.