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

artisticre's avatar

Array to string conversion

I am struggling with inserting multiple checkbox values. Below is my form for the checkboxes and my submit controller function. I know this isn't right but I really don't know what to do. Can someone please point me in the right direction?

Form

<div class="row">
  <div class="col-md-7 pl-0">
  <label for="email" class="font-weight-bold m-t-10 m-r-10">Type of employment desired:</label>
  <div class="form-check-inline">
  <label class="form-check-label">
    <input type="checkbox" class="form-check-input" name="emptype[]" value="fulltime">Full Time
  </label>
</div>
<div class="form-check-inline">
  <label class="form-check-label">
    <input type="checkbox" class="form-check-input"  name="emptype[]" value="parttime">Part Time
  </label>
</div>
<div class="form-check-inline">
  <label class="form-check-label">
    <input type="checkbox" class="form-check-input"  name="emptype[]" value="seasonal">Seasonal
  </label>
</div>


  </div>

</div>

controller

ublic function submit(Request $request)
    {
    

    $app = Application::create($request->all() + [
        'user_id' => Auth::user()->id,
        $app['emptype'] = $request->input('emptype')
        
    ]);


    return Redirect::to(URL::previous())->with('success', 'Your employment application has been submitted!');
   
       
    }
0 likes
6 replies
jlrdw's avatar

If storing in one field, look up the implode function in the PHP manual.

artisticre's avatar

This is what I used

'emptype' => implode(',',$request->emptype)

If I post dd($request->emptype) I get

array:2 [▼
  0 => "fulltime"
  1 => "parttime"
]

But still get the Array to string conversion error

warpig's avatar

Try removing [] from your name="" value? - have you tried that? I was in the same situation just a few days ago, hehe :-)

But mine was more about a single value, "1" or "0" inside a checkbox, what you're trying to do is select 1 or more and pass those into a column database, correct?

artisticre's avatar

I have tried a few different things. I just do not understand this. Here is what I tried.

 public function submit(Request $request)
    {
    
        $request->validate([
            'dateavailable' => 'required|date',
            'citizen' => 'required',
            'workpapers'=> 'required',

        ]);
         
    $app = Application::create($request->all() + [
        'user_id' => Auth::user()->id,
         'emptype' => implode(",", $request->input('emptype')),
          
    ]);


    return Redirect::to(URL::previous())->with('success', 'Your employment application has been submitted!');
   
       
    }
artisticre's avatar
artisticre
OP
Best Answer
Level 5

I figured it out. In my Model I put

protected $casts = [
        'emptype' => 'array',
    ];

and in the controller


    public function submit(Request $request)
    {
    
        $request->validate([
            'dateavailable' => 'required|date',
            'citizen' => 'required',
            'workpapers'=> 'required',
            'emptype' => 'required',

        ]);
         
           $emp = array($request->emptype);
     
    $app = Application::create($request->all() + [
        'user_id' => Auth::user()->id,
        'emptype' => json_encode($emp),
          
    ]);


    return Redirect::to(URL::previous())->with('success', 'Your employment application has been submitted!');
   
       
    }

Please or to participate in this conversation.