1

I have created a form with 3 checkboxes and when I check one checkbox it successfully puts it into a database. But when I checks more than one checkbox, just one data input makes it into the database, which is the first checkbox data. How to store the data input from all checkboxes ?

These are my codes so far :

HTML

<div class="form-group">
  <b>Paket</b>
  <br/>
  <fieldset>
    <input type="checkbox" name="paket" id="delux" value="Delux"> <label for="">Paket Delux </label>
    <input type="checkbox" name="paket" id="p1" value="Paket1"> <label for="">Paket 1</label>
    <input type="checkbox" name="paket" id="p2" value="Paket2"> <label for="">Paket 2</label>
  </fieldset>
</div>

Controller

public function proses(Request $request)
    {
        $this->validate($request,[
            'file'=>'required|file|image|mimes:jpeg,png,jpg|max:2048',
            'makanan'=>'required',
            'jenis'=>'required',
            'paket'=>'required',
        ]);

        $file = $request->file('file');
        $nama_file = time()."_".$file->getClientOriginalName();

        //nama folder tempat file diupload
        $tujuan_upload = 'image_file';
        $file->move($tujuan_upload,$nama_file);

        Gambar::create([
            'file' =>$nama_file,
            'nama_makanan'=>$request->makanan,
            'kode_jenis'=>$request->jenis,
            'paket'=>$request->paket,
        ]);
        return redirect('/makanan');
    }
1
  • name="paket" update name="paket[]" and 'paket'=>$request->paket, with 'paket' => implode(',',$request->paket) Commented Sep 20, 2019 at 5:29

2 Answers 2

1

Try this : Update your all 3 checkbox names

From:

name="paket" 

to:

 name="paket[]" 

and

'paket'=>$request->paket,

to:

'paket' => implode(',',$request->paket`)

This will help you out :)

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

1 Comment

A good answer includes an explanation of why they need to update the code, what it solves and how.
0
<div class="form-group">
                    <b>Paket</b>
                    <br/>
                    <fieldset>
                        <input type="checkbox" name="paket[]" id="delux" value="Delux"> <label for="">Paket Delux </label>
                        <input type="checkbox" name="paket[]" id="p1" value="Paket1"> <label for="">Paket 1</label>
                        <input type="checkbox" name="paket[]" id="p2" value="Paket2"> <label for="">Paket 2</label>
                    </fieldset>
                </div>

public function proses(Request $request)
    {
        $this->validate($request,[
            'file'=>'required|file|image|mimes:jpeg,png,jpg|max:2048',
            'makanan'=>'required',
            'jenis'=>'required',
            'paket'=>'required',
        ]);

        $file = $request->file('file');
        $nama_file = time()."_".$file->getClientOriginalName();

        //nama folder tempat file diupload
        $tujuan_upload = 'image_file';
        $file->move($tujuan_upload,$nama_file);

        Gambar::create([
            'file' =>$nama_file,
            'nama_makanan'=>$request->makanan,
            'kode_jenis'=>$request->jenis,
            'paket'=>json_encode($request->paket),
        ]);
        return redirect('/makanan');
    }

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.