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.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
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.