未知栏' $$ hashKey'在'字段列表'使用Angular时

时间:2018-01-20 15:11:56

标签: laravel

我正在向Laravel API应用程序提交一个AngularJS表单,其形式是我有多个输入字段,其名称可以帮助我在Laravel中将它们作为数组接收:

operations[name]
operations[cost]
operations[operator]
operations[date]
..
..

提交后的结果是这样发送的:

operations[0][$$hashKey]    object:554
operations[0][operation]    t1
operations[0][operator] r1
operations[0][cost] 12
operations[0][count]    2
operations[0][date] Tue Jan 02 2018 00:00:00 GMT+0300
operations[1][$$hashKey]    object:552
operations[1][operation]    t2
operations[1][operator] r2
operations[1][cost] 122
operations[1][count]    3
operations[1][date] Mon Jan 08 2018 00:00:00 GMT+0300

我正在尝试批量插入数据库中的操作字段,如下所示:

Operation::insert($request->input('operations')); 

但AngularJS正在添加一个名为$$hashKey的额外字段,该字段在我的数据库列中不存在,因此我收到此错误:

1054 Unknown column '$$hashKey' in 'field list'

这是常见的,我搜索了很多解决方案,要么从$request中移除额外的字段,要么忽略它的任何其他方式,但是没有它们工作,我试过的方法之一:

$request = $request->except('operations[$$hashKey]');

但它也会引发错误:

Call to a member function input() on array

我很困惑,我很感激帮助我解决问题。

1 个答案:

答案 0 :(得分:1)

对于$request->except('operations[$$hashKey]');,你必须使用像$request->except('operations.$$hashKey');这样的点符号,但是你有这样的结构:

operations[0][$$hashKey]    object:554
operations[0][operation]    t1
operations[0][operator] r1
operations[0][cost] 12
operations[0][count]    2
operations[0][date] Tue Jan 02 2018 00:00:00 GMT+0300
operations[1][$$hashKey]    object:552
operations[1][operation]    t2
operations[1][operator] r2
operations[1][cost] 122
operations[1][count]    3
operations[1][date] Mon Jan 08 2018 00:00:00 GMT+0300

那么这种方法对你没有帮助,因为你做不到 每个元素$request->except('operations.*.$$hashKey');。所以我建议你试试这个:

$operations = collect($request->get('operations'));
    $operations->transform(function($item){
        return array_except($item, '$$hashKey');
    });
Operation::insert($operations->toArray());