1

How to save multiple checkbox value into database? now the value is storing in different rows. example: i selected value 1 and 2.so the value 1 in 1 row and value 2 in another row.

blade

  <form enctype="multipart/form-data" action="/report/{{$postqs->id}}" 
     method="POST"> 

   <input type="checkbox" name="item[]" value="one" />1
   <input type="checkbox" name="item[]" value="two" />2
   <input type="checkbox" name="item[]" value="three" />3


 <div class="row">
<div class="form-group">
<label class="control-label col-sm-2"> Others:</label>
<div class="col-sm-7">
 <textarea name="report" id="report" class="form-control"></textarea>
 </div>
 </div>
 <input type="hidden" name="_token" value="{{ csrf_token() }}">
 <input type="submit" class="pull-right btn btn-sm btn-primary">
 </div></div></div>

</form>

Report model

protected $fillable = ['report','postqs_id','user_id','item'];

Store controller :

   public function store(Request $request,Postqs $postqs,$id,Report $report, 
   Admin_report $admin_report)
{

    foreach ($request->input("item") as $key=>$value){
        $add_item = new Report;
         $add_item->item= $value;
         $add_item->user_id= Auth::user()->id;
         $add_item->postqs_id=$id;
         $add_item->save();

        return back();

    }
3
  • dd($request->input("item")) what its give you and what exactly happening, are you saving any data to DB currently with this code. Commented Jan 17, 2018 at 7:58
  • array:2 [▼ 0 => "one" 1 => "two" ] this is what im getting if i select the checkbox and im getting this error :SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into report (item, updated_at, created_at) values (one, 2018-01-17 08:08:02, 2018-01-17 08:08:02)) Commented Jan 17, 2018 at 8:08
  • i have report,item,user_id,postqs_id field in database. report and item default value is null. if i remove the foreach i can save the report,user_id,postqs_id but if i add the foreach to save the checkbox value its showing that error. Commented Jan 17, 2018 at 8:10

2 Answers 2

1

You should separate items as key and value so that you can save the value and the error it gives you shows that it needs user_id, so add the user_id to it., and your code should look like this:

foreach ($request->input("item") as $key=>$value){
       $add_item = new Report;
        $add_item->item= $value;
        $add_item->user_id= Auth::user()->id;
        $add_item->postqs_id=$id;
        $add_item->save();
}

Because you want the value of the checkboxes, not the key. In the way you do it, it saves key and value together in the item column.

#Update

In this situation (saving array into DB) you don't put item input into foreach loop, and just save it as what it is, before that you must tell to your model to treat that as an array by putting $casts property into Report model:

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

This way it should save as array into items column. after that you just save items like this code below:

        $add_item = new Report;
        $add_item->item= $request->input("item");
        $add_item->user_id= Auth::user()->id;
        $add_item->postqs_id=$id;
        $add_item->save();

Note that there is no need for foreach loop in this scenario.

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

10 Comments

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'postqs_id' cannot be null (SQL: insert into report (item, user_id, postqs_id, updated_at, created_at) values (three, 5, , 2018-01-17 08:20:16, 2018-01-17 08:20:16)) im getting this error now . even i added this code inside foreach : $add_item->postqs_id=postqs->id
@kavi first you should write the code write $add_item->postqs_id=$postqs->id, you missed the $ sign second you pass the $postqs->id via Url as $id. you should write $add_item->postqs_id=$id
@kavi did it fixe the problem or not?
user_id fixed and now having problem for postqs_id .
@kavi show us exactly the code you have right now. update your question and show us your updated code. and write the error after it.
|
0

It seems you need to add user_id to insert a new Report.

$user = auth()->user();


foreach ($request->input("item") as $item){
    $add_item = new Report;
    $add_item->user_id= $user->id;
    $add_item->item= $item;
    $add_item->save();
}

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.