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

pablozera's avatar

Save multiple checkboxes to the database

view.blade.php

 <div class=" ">
<div class="checkbox">
    <label class="checkbox">
        <input name="q9[]" type="checkbox" value="Diário" />
        Diário
    </label>
</div>
<div class="checkbox">
    <label class="checkbox">
        <input name="q9[]" type="checkbox" value="Semanal" />
        Semanal
    </label>
</div>
<div class="checkbox">
    <label class="checkbox">
        <input name="q9[]" type="checkbox" value="Quinzenal" />
        Quinzenal
    </label>
</div>
<div class="checkbox">
    <label class="checkbox">
        <input name="q9[]" type="checkbox" value="Mensal" />
        Mensal
    </label>
</div>
<div class="checkbox">
    <label class="checkbox">
        <input name="q9[]" type="checkbox" value="Outro" />
        Outro
    </label>
</div>
 </div>
 </div>

I have some checkbox fields. And I have other radio and input fields.

When trying to save this data in the database, it presents this error

ErrorException: Array to string conversion
 public function store(Request $request) {
        $data = $request->input();
        $data['user_id'] = Auth::id();
        Coordenador::create($data);
}

If I remove [] from the field, I will be able to save only one checkbox option in the database. And I wanted to save all the checkbox marked fields. What can I do to fix this array error?

0 likes
8 replies
psylogic's avatar

Do you want to save the answers in same column as a string or json ? or each answer in new row ?

pablozera's avatar

It can be in the same column. I even tried to use implode, but because of user_id I was unable to use the create function.

psylogic's avatar

if you have relation between Coordenador and User

you can use auth()->user()->cordenator()->save($data)

of course after using implode function on $request->q9

pablozera's avatar

My model

 public function user(){
        return $this->belongsTo(User::class, 'user_id');
    }

show me an example of what this code would look like

psylogic's avatar
psylogic
Best Answer
Level 7

User modal

I guess you will have hasMany(Coordenador::class)

by using this line laravel will automatically inject user_id with auth id into cordenator table

$data['q9'] = implode(",",$data['q9']);
auth()->user()->cordenator()->create($data);

or in the way you use

public function store(Request $request) {
        $data = $request->input();
        $data['user_id'] = Auth::id();
        $data['q9'] = implode(",",$data['q9']);
        Coordenador::create($data);
}

kingmaker_bgp's avatar

If you wish to save those checkbox values as Comma separated string in the Database, you can use the following code.

public function store(Request $request) {
    // TODO do the validation
    Coordenador::create([
        "q9" => $request->q9 ? implode(',', $request->q9) : null,
        "user_id" => auth()->id()
        // Other Stuffs
    ]);

    // TODO send Response
}
pablozera's avatar

Nice. It worked! It looks elegant. Thanks!

1 like

Please or to participate in this conversation.