如何为 Laravel 批量 updateOrCreate() 准备 PHP 多维数组?
How to prepare PHP multidimensional arrays for Laravel Mass updateOrCreate()?
我正在尝试批量插入 Laravel (5.6) 模型。阵列准备有问题;它returns
lluminate \ Database \ QueryException (42S22)
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'
(SQL: insert into `_geolocation` (`0`, `1`, `2`, `3`, `4`) values
(Manchester,+UK, Manchester,+UK, Manchester,+UK, Manchester,+UK,
Manchester,+UK))
我正在生成这样的数组:
if($outputTo->status->message == "OK"){ // if
for ($i = 0; $i < $outputTo->total_results; $i++) {
//print_r($outputFrom->results[$i]->geometry->lat);
$geodata_b [] =[
$formattedAddrTo,
$outputTo->results[$i]->formatted,
$outputTo->results[$i]->geometry->lat,
$outputTo->results[$i]->geometry->lng,
];
}
}
这是我的阵列的输出;
array:5 [▼
0 => array:4 [▼
0 => "Manchester,+UK"
1 => "Manchester, Greater Manchester, England, United Kingdom"
2 => 53.4791301
3 => -2.2441009
]
1 => array:4 [▼
0 => "Manchester,+UK"
1 => "Chapel Street Primary School, Chapel Street, Manchester M19 3GH, United Kingdom"
2 => 53.443957
3 => -2.1858478
]
2 => array:4 [▼
0 => "Manchester,+UK"
1 => "The Manchester, Lytham Road, Bispham FY1 6AH, United Kingdom"
2 => 53.8067298
3 => -3.0549142
]
3 => array:4 [▼
0 => "Manchester,+UK"
1 => "The Manchester, Bromsgrove Road, Bromsgrove B62 0HH, United Kingdom"
2 => 52.4003403
3 => -2.0539726
]
4 => array:4 [▼
0 => "Manchester,+UK"
1 => "The Harlequin, Spitalfields, Sheffield S3 8GG, United Kingdom"
2 => 53.3884864
3 => -1.4663405
]
]
正在插入代码;
$geolCache = \App\Geolocation::updateOrCreate([$geodata_b]);
我的模型;
protected $fillable = ['adrr','faddr','lat','lng'];
谁能帮我解决这个问题?
您没有关注 key-value 对。试试这个代码
if($outputTo->status->message == "OK"){ // if
for ($i = 0; $i < $outputTo->total_results; $i++) {
$geodata_b [] = [
'adrr' => $formattedAddrTo,
'faddr' => $outputTo->results[$i]->formatted,
'lat' => $outputTo->results[$i]->geometry->lat,
'lng' => $outputTo->results[$i]->geometry->lng,
];
}
}
我正在尝试批量插入 Laravel (5.6) 模型。阵列准备有问题;它returns
lluminate \ Database \ QueryException (42S22)
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'
(SQL: insert into `_geolocation` (`0`, `1`, `2`, `3`, `4`) values
(Manchester,+UK, Manchester,+UK, Manchester,+UK, Manchester,+UK,
Manchester,+UK))
我正在生成这样的数组:
if($outputTo->status->message == "OK"){ // if
for ($i = 0; $i < $outputTo->total_results; $i++) {
//print_r($outputFrom->results[$i]->geometry->lat);
$geodata_b [] =[
$formattedAddrTo,
$outputTo->results[$i]->formatted,
$outputTo->results[$i]->geometry->lat,
$outputTo->results[$i]->geometry->lng,
];
}
}
这是我的阵列的输出;
array:5 [▼
0 => array:4 [▼
0 => "Manchester,+UK"
1 => "Manchester, Greater Manchester, England, United Kingdom"
2 => 53.4791301
3 => -2.2441009
]
1 => array:4 [▼
0 => "Manchester,+UK"
1 => "Chapel Street Primary School, Chapel Street, Manchester M19 3GH, United Kingdom"
2 => 53.443957
3 => -2.1858478
]
2 => array:4 [▼
0 => "Manchester,+UK"
1 => "The Manchester, Lytham Road, Bispham FY1 6AH, United Kingdom"
2 => 53.8067298
3 => -3.0549142
]
3 => array:4 [▼
0 => "Manchester,+UK"
1 => "The Manchester, Bromsgrove Road, Bromsgrove B62 0HH, United Kingdom"
2 => 52.4003403
3 => -2.0539726
]
4 => array:4 [▼
0 => "Manchester,+UK"
1 => "The Harlequin, Spitalfields, Sheffield S3 8GG, United Kingdom"
2 => 53.3884864
3 => -1.4663405
]
]
正在插入代码;
$geolCache = \App\Geolocation::updateOrCreate([$geodata_b]);
我的模型;
protected $fillable = ['adrr','faddr','lat','lng'];
谁能帮我解决这个问题?
您没有关注 key-value 对。试试这个代码
if($outputTo->status->message == "OK"){ // if
for ($i = 0; $i < $outputTo->total_results; $i++) {
$geodata_b [] = [
'adrr' => $formattedAddrTo,
'faddr' => $outputTo->results[$i]->formatted,
'lat' => $outputTo->results[$i]->geometry->lat,
'lng' => $outputTo->results[$i]->geometry->lng,
];
}
}