2

I am trying to insert data into database. But i don't know how to insert multi select checkbox data into the mysql database.

  • Controller

    public function create(Request $request)
        {
            try { 
                
                $id_student = $request->get('id_student');
                   
                $consecutive = DB::select('SELECT SUM(idRecord) FROM record GROUP BY idRecord');
                $final_consecutive = sprintf("%04d", $consecutive); 
        
                foreach($request->select as $data)
                {
                    Records::create($data);
                }
    
                return back()->with('success', 'Constancia creada correctamente');
        
            } catch (\Illuminate\Database\QueryException $e) {
                $message = $e->getMessage();
                if (strpos($message, "Duplicate entry")) {
                    return back()->with('err', 'Esta constancia ya ha sido creada');
                }
                if (strpos($message, "1366 Incorrect integer value: '' for column 'idGrupo'")) {
                    return back()->with('err', 'Debe seleccionar un grupo para poder continuar');
                }
                return back()->with('err', $message);
            }
        }

  • View

    @foreach ($group->students as $student)
        <tr>
           <td class="align-middle text-center">
               <input class="form-control" type="text" name="nombre" value="{{$student->lastName}} {{$student->Names}}" disabled>
            </td>
    
            <td class="align-middle text-center">
                 <input class="form-control form-control-sm" type="text" name="grade" value="{{isset($student->pivot->grade)?$student->pivot->grade:''}}" placeholder="grade" disabled>
             </td>
    
    
            <form action="{{url('/Create/Records')}}" method="POST">
                <input class="form-control form-control-sm" type="hidden" name="id_student" value="{{$student->id_student}}" >
                    <td class="align-middle text-center">
                        <input id="select" type="checkbox" name="select[]">
                    </td>
        </tr>
    @endforeach
    </tbody>
    </table>
        <div class="d-flex justify-content-center">
            <button type="submit" class="btn btn-sm btn-outline-success">Save</button>
        </div>

What I tried to do was use a select checkbox and then in the Controller I passed it as an array in the foreach loop, but honestly I think I'm nowhere close to figuring it out... Other than that the consecutive is automatically generated and I don't know how to pass it to the array as well.

I get this error by the way:

Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, string given

records

2
  • If you check your for loop, $request->select it will give you a string, not an key value pair array. That's why it is throwing this error. Can you share Record model structure? Commented Mar 8, 2022 at 6:39
  • Ok, I edited the post and added the image of my table records. Commented Mar 8, 2022 at 6:49

3 Answers 3

1

You should pass data to your Model like:

Records::create(['id_student' => $idStudent, 'consecutive' => $consecutive]);

Currently, you are giving like:

Records::create('string');

Which is not correct way to pass the data. That's why getting the error.

Sign up to request clarification or add additional context in comments.

Comments

0

Create() take array as parameter. When your foreach loop execute $data value 0 or 1 as string so that why error is given.

if you want to create in foreach loop then

foreach($request->select as $data)
{
    Records::create(['field_name' => $data]);
}

Comments

0

by default multiple select checkbox will be converted to assoc array with name that ends with [] as its key for example your using so you can access it like:

$select = $_POST['select'];

then you can iterate through the values this is a plain php would suggest to use the laravel method as it offers more like security and filter this one is a simple and fast solution. But if you using an ajax submission you should always make sure that the parameter always ends with [] example you have a get request but still the same syntax applies to post method:

http://localhost?select[]=1&select[]=2

the same code will work as on top but for get request like this you will use:

$select = $_GET['select'];

haven't tried on laravel request object but I think the same conversion is being done when your using the Request instance you may try that as well just make sure you always end the parameter name with [] but if the name of all the checkbox is set to select it will just return a single string which the error means.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.