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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化