将复选框值保存到 Laravel 中的数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39615677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:29:18  来源:igfitidea点击:

saving check box values to the database in laravel

phplaravelcheckbox

提问by Chamara Abeysekara

I'm new to laravel. I'm saving some checkbox value to the database using loop the loop work but it only saves the last value in the array to the database.

我是 Laravel 的新手。我正在使用 loop 循环工作将一些复选框值保存到数据库中,但它只将数组中的最后一个值保存到数据库中。

this is my form

这是我的表格

<form action="{{url('resortf')}}" method="post" enctype="multipart/form-data">
            <input hidden name="h_id" value="{{ $hotel->id}}">
            @foreach($facility as $facilities)
            <div class="col-md-4">
                <img src="{{$facilities->image}}" width="50px" height="50px;">
                <label>{{$facilities->name}}</label>
                <input type="checkbox" value="{{$facilities->id}}" name="facilities[]">
            </div>
            @endforeach
            <div class="row">
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                <input type="submit" class="btn btn-success" value="Next">
                <input type="reset" class="btn btn-success" value="Reset">
            </div>

        </form>

form working fine; $facilitiesand $hotelare passed from the controller.

表格工作正常;$facilities$hotel从控制器传递。

this is the store function

这是商店功能

public function store(Request $request) {
    $resortfacility = new Resortfacility;
    $loop = $request->get('facilities'); 

    foreach ($loop as $value){
        $resortfacility->h_id = $request->get('h_id');
        $resortfacility->f_id = $value;

        $resortfacility->save();

    }
}

is there any other way to do this that works?

有没有其他方法可以做到这一点?

采纳答案by Khallister

Your problem occurs because you create one instance of Resortfacility, and then you fill in its values and save. The problem is, every time you perform changes to this object in a loop, you are updatingexisting object and that's why there is only one record in your database, the last one from the loop.

出现问题是因为您创建了 的一个实例Resortfacility,然后填写了它的值并保存。问题是,每次在循环中对该对象执行更改时,您都是updating现有对象,这就是为什么数据库中只有一条记录,即循环中的最后一条记录。

Try making new instance of Resortfacility inside the loop like this:

尝试在循环中创建新的 Resortfacility 实例,如下所示:

public function store(Request $request) {
    $loop = $request->get('facilities');
    foreach ($loop as $value){
        $resortfacility = new Resortfacility;
        $resortfacility->h_id = $request->get('h_id');
        $resortfacility->f_id = $value;
        $resortfacility->save();
    }
}

回答by Sanzeeb Aryal

The other answer is correct but, bulk insertion might be helpful, as creating new object within foreach loop will make query for every record.

另一个答案是正确的,但批量插入可能会有所帮助,因为在 foreach 循环中创建新对象将对每条记录进行查询。

     public function store(Request $request) 
     {
             $facilities = $request->get('facilities');
             $data=array();
             foreach($facilities as $facility)
             {
                 $data[] =[
                            'f_id' => $facility,
                            'h_id' => $request->get('h_id'),
                         ];                 
             }
        }

 Resortfacility::insert($data);