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

Mick79's avatar

Add checkbox array to database

I have a form that has various elements and a checkbox area where the user can select multiple boxes.

I am capturing the relevant checkbox data in an array and that's fine.

I am really struggling with adding this to my database in the controller.

anyone have any tips?

Updated with more info. Apologies.

My form:

<form action="createSB" method="post">
    {{csrf_field()}}

    <input class="sbInput mt-20" type="text" name="recipientName" placeholder="Enter name of recipient" required/>

    <input class="sbInput mt-20" type="text" name="recipientEmail" placeholder="Enter Email of recipient" required/>
    @foreach($tracks as $track)

        <input type="checkbox" name="trackid[]" value="{{$track->id}}">
        <input type='hidden' name='tracksource[]' value='{{$track->tracksource}}'>
        <p>{{$track->track}}</p>

    @endforeach

</form>

And here is what I'm trying to do in the controller:

public function create(Request $request)
{

$user = Auth::user();
$token = str_random(50);

 SB::create([

                'user_id'        => $user->id,
                'recipientEmail' => request('recipientEmail'),
                'recipientName'  => request('recipientName'),
                'token' => $token,

            ]);

  SBTracks::create([

                'track_id'     => request('trackid'),
                'track_source' => request('tracksource'),
                'user_id' => $user->id,
                'token' => $token,

            ]);

}

As you can see I am trying to write to two different models in one controller. Is this an issue?

0 likes
4 replies
Chris1904's avatar

If you simply want to store the array, you can json decode it and store in inside a json column.

You could also store it as different booleans - each their own column.

RamjithAp's avatar

Merge your checkbox data array with the comma and make it as a string. Then save it into your database while access data use the same way explode the string with comma.

//Assuming your form looks like below
<input type="checkbox" name="products[]" value="Mobile">
<input type="checkbox" name="products[]" value="Laptop">

Do the following in the method of the controller:

public function store(ProductRequest $request)
{
    // merge your array with comma
      $products= implode(",", $request->get('products'));

    // $products should return a value of 'mobile, laptop' check it out just in case.
    
    // You can probably do something with $request->merge() to cut down on individual values in the array and cut down on the code here.  

    $order= Order::create([
        'name' => $request->get('name'),
        'cat_id' => $request->get('cat_id'),
        'tags' => $request->get('tags'),
        'products' => $products
    ]);
    
    if($order) 
    {
        \Session::flash('flash_message','created!!');
    } else {
        \Session::flash('flash_message','Error!');
    }
    
    return redirect('/orders');
}

While taking back the values use explode function

$order = Order::find($id);
$products= explode(",", $order ->products));
return $products;
foreach(  $products as $product){
<li>{{product}}</li>
}
Mick79's avatar
Mick79
OP
Best Answer
Level 5

I solved this myself. I had to wrap the insert query in a foreach and increment the array values on each run of the loop. I have no idea if this is best practice or not but it works.

$x = 0;

            foreach ($request->trackid as $track)
            
        {

                SBTracks::create([
                    'user_id'      => $user->id,
                    'track_id'     => $request->trackid[$x],
                    'track_source' => $request->tracksource[$x],
                    'token'        => $token,
                    'sb_id'   => 001,
                ]);

                $x++;
            }

Please or to participate in this conversation.