Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to save my results in the database but im getting an error exception.

In my view I have a radio button(array) that gets the result of each student which is present,late,absent,others

Here's my view

  <td>{{ $users->student_id  }} </td>
  <td>{{ $users->student_firstname }} {{ $users->student_lastname }}</td> 
  <td>{{ Form::radio('result['.$users->student_id.']', 'present' , true) }}</td>
  <td>{{ Form::radio('result['.$users->student_id.']', 'late' ) }}</td>
  <td>{{ Form::radio('result['.$users->student_id.']', 'absent') }}</td>
  <td>{{ Form::radio('result['.$users->student_id.']', 'others') }}</td>

Here's my controller

  $attendance = new Attendances();
  $attendance->status = Input::get('result');
  $attendance->comment = Input::get('comment');
  $attendance->save();
like image 731
Virgil Joseph Cruz Avatar asked Dec 09 '22 03:12

Virgil Joseph Cruz


2 Answers

Your radio input result will return an array, due to the naming convention you have chosen.

If you wish to save the singular value of the input result, use the following format.

View code:

<td>{{ Form::radio('result', 'present' , true) }}</td>
<td>{{ Form::radio('result', 'late') }}</td>
<td>{{ Form::radio('result', 'absent')   }}</td>
<td>{{ Form::radio('result', 'others')  }}</td>

If you are expecting multiple values in an array, then you should be looping through the code, and saving each individually:

Controller Code:

foreach (Input::get('result') as $studentId=>$value)
{
     $attendance = new Attendances();
     $attendance->status = $value;
     $attendance->comment = Input::get('comment');
     //We should save the student id somewhere.
     $attendance->student_id = $studentId;
     $attendance->save();
}

Suggestion:

If you wish to save several student's information on the same form, the suggestion below will work great.

View Code:

<td>{{ $users->student_id  }} </td>
<td>{{ $users->student_firstname }} {{ $users->student_lastname }}</td> 
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'present' , true) }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'late' ) }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'absent') }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'others') }}</td>
<td>{{ Form::text('student['.$users->student_id.'][comment]'}}</td>

Controller Code for saving

//We loop through each student and save their attendance.
foreach (Input::get('student') as $studentId=>$data)
{
     $attendance = new Attendances();
     $attendance->status = $data['status'];
     $attendance->comment = $data['comment'];
     //We should save the student id somewhere.
     $attendance->student_id = $studentId;
     $attendance->save();
}
like image 167
Mysteryos Avatar answered Dec 11 '22 08:12

Mysteryos


I had a similar error, where using

$attendance->fill(array with non-existing columns);

sets a non-existing column value, so on calling

$attendance->save();

afterwards, it will throw that error

like image 25
cnlevy Avatar answered Dec 11 '22 07:12

cnlevy