如何使用laravel原始查询和参数插入到表中?

q8l4jmvw  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(360)
DB::table('my_table')->insertGetId([
    ...
    'code' => $data['code'],
    'geopoint' => \DB::raw('POINT(?, ?)', [$data['lat'], $data['lng']]),
    ...
]);

它回来了

Invalid parameter number (SQL: insert into `residence` (`code`, values (POINT(, ), , ?, ?)

我怎样才能解决这个问题?
谢谢

voase2hg

voase2hg1#

这样试试
为要插入的值创建数组,并应用与表中字段名相同的键

$point = DB::select("SELECT POINT($data['lat'], $data['lng']) as point FROM `any_table_name_avilable_in_your_db` LIMIT 1"); 

$data=array(
    "lat_field_name_table"=>$data['lat'],
    "lng_field_name_table"=>$data['lng'],
    "location"=>$point[0]->point
);

$checkinsert=DB::table('mytablename')->insert($data);

检查你的数据是这样插入的吗

if($checkinsert>0){
    //data inserted 
   //do whatever you want if inserted
}else{
   //data not inserted 
   //do whatever you want if not inserted
}

或者像这样:

$point = DB::select("SELECT POINT($data['lat'], $data['lng']) as point FROM any_table_name_avilable_in_your_db LIMIT 1"); 

DB::table('my_table')->insertGetId([ 
... 
'code' => $data['code'], 
'geopoint' =>$point[0]->point , 
... 
]);

相关问题