php - Laravel 5.0: How to update an array of data: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array -
i absolute beginner of laravel. dealing error "preg_replace(): parameter mismatch, pattern string while replacement array" when try update array of data.
does know way solve error? if need more information, please leave comments.
any advice appreciated. in advance!
logscontroller.php
public function update(createlogrequest $request, $course_id){ $count = count($request->input('weeks')); $input = $request->all(); $logs = array(); ($i = 0; $i < $count; $i++){ if($input['weeks'][$i]){ array_push($logs, array( 'user_id' => \auth::user()->id, 'course_id' => $course_id, 'weeks' => $i + 1, 'work_description' => $input['work_description'][$i], 'instructor_comments' => $input['instructor_comments'][$i], 'status' => $input['status'][$i], 'created_at' => carbon::now(), 'updated_at' => carbon::now(), )); } } $log->update($logs); return redirect('/student/home'); } when put code dd($logs), result below.
array:2 [▼ 0 => array:8 [▼ "user_id" => "1" "course_id" => "39" "weeks" => 1 "work_description" => "fadfad" "instructor_comments" => "fdasfda" "status" => "accepted" "created_at" => carbon {#219 ▶} "updated_at" => carbon {#212 ▶} ] 1 => array:8 [▼ "user_id" => "1" "course_id" => "39" "weeks" => 2 "work_description" => "fadsfad" "instructor_comments" => "fdasfdasfad" "status" => "accepted" "created_at" => carbon {#218 ▶} "updated_at" => carbon {#222 ▶} ] ] log_edit.blade.php
{!! form::hidden('course_id', $course->id) !!} @foreach($logs $log) <tbody> <tr> <td> {{ $log->weeks }} {!! form::hidden('weeks[]', $log->weeks) !!} </td> <td> {!! form::textarea('work_description[]', $log->work_description) !!} </td> <td> {!! form::textarea('instructor_comments[]', $jlog->instructor_comments) !!} </td> <td> {!! form::select('status[]', array('accepted' => 'accepted', 'pending' => 'pending', 'declined' => 'declined', ), $log->status) !!} </td> </tr> @endforeach </tbody>
here problem:
$log->update($logs); update method not taking multidimensional array.
your logscontroller.php should this:
public function update(createlogrequest $request, $course_id, log $log){ $input = $request->all(); foreach ($input['weeks'] $i => $log_id){ $data = [ 'user_id' => \auth::user()->id, 'course_id' => $course_id, 'weeks' => $i + 1, 'work_description' => $input['work_description'][$i], 'instructor_comments' => $input['instructor_comments'][$i], 'status' => $input['status'][$i], 'created_at' => carbon::now(), 'updated_at' => carbon::now(), ]; $log->where('id', $log_id)->update($data); } return redirect('/student/home'); }
Comments
Post a Comment