doujiao2014 2018-01-17 07:48
浏览 97
已采纳

如何以数组形式保存复选框值?

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();

    }
  • 写回答

2条回答 默认 最新

  • dqg63264 2018-01-17 08:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?