对于Laravel中的每个循环

三户

我的数据库结构如下 driver_location table

在此处输入图片说明

从此表中,我获取附近距离在5公里之内且在线状态与laravel中的以下查询相同的驾驶员ID

 $nearest_car = DriverLocation::select(DB::raw('*, ( 6371 * acos( cos( radians('.$pickup_latitude.') ) * cos( radians( latitude ) ) * cos(radians( longitude ) - radians('.$pickup_longitude.') ) + sin( radians('.$pickup_latitude.') ) * sin( radians( latitude ) ) ) ) as distance'))
->having('distance', '<=', 5)->where('driver_location.status','Online')->where('driver_location.car_id',$car_id);

现在,我要对来自driver_location表的user_id的每个附近执行一个请求该请求将被插入到bid_request表中,表限于5个附近的驱动程序。我尝试过如下所示的for循环,但它多次从驱动程序位置表中插入同一user_id的记录。我需要每个用户一个请求

 for($i=0;$i<5;$i++){
    DB::table('bid_request')->insert(
    array(
        'user_id'     =>   $rider_id, 
        'group_id'   =>   null,
         'pickup_latitude'     => $pickup_latitude, 
        'pickup_longitude'   =>   $pickup_longitude,
         'drop_latitude'     =>   $drop_latitude, 
        'drop_longitude'   =>    $drop_longitude,
         'driver_id'     =>   $nearest_car->user_id, 
        'car_id'   =>   $car_id,
         'pickup_location'     =>   $pickup_location, 
        'drop_location'   =>    $drop_location,
         'payment_mode'     =>   $payment_method_store, 
        'status'   =>   'Pending',
         'is_wallet'     =>    $is_wallet, 
        'driver_mobile'   =>    $driver_details->mobile_number,
         'driver_avg_ratings'     =>   $driver_details->driver_avg_ratings, 

        'distance'   =>   $dis,
        'min_time'   =>   $get_near_car_time

        //minimum time and distance needs to b
       )
     );

    }
 }// end $is_wallet!='Bid'
亚辛·帕特尔(Yasin Patel)

如果要在bid_request表中插入5条记录,而不是从下面的查询中仅获得5条记录:

$nearest_car = DriverLocation::select(DB::raw('*, ( 6371 * acos( cos( radians('.$pickup_latitude.') ) * cos( radians( latitude ) ) * cos(radians( longitude ) - radians('.$pickup_longitude.') ) + sin( radians('.$pickup_latitude.') ) * sin( radians( latitude ) ) ) ) as distance'))
->having('distance', '<=', 5)
->where('driver_location.status','Online')
->where('driver_location.car_id',$car_id)
->limit(5)->get();

现在您需要记录阵列以插入表中:

$data = [];
foreach ($nearest_car as $key => $value) {
  $data[] = [
    'user_id'     =>   $rider_id,
    'group_id'   =>   null,
    'pickup_latitude'     => $pickup_latitude,
    'pickup_longitude'   =>   $pickup_longitude,
    'drop_latitude'     =>   $drop_latitude,
    'drop_longitude'   =>    $drop_longitude,
    'driver_id'     =>   $value->user_id,
    'car_id'   =>   $car_id,
    'pickup_location'     =>   $pickup_location,
    'drop_location'   =>    $drop_location,
    'payment_mode'     =>   $payment_method_store,
    'status'   =>   'Pending',
    'is_wallet'     =>    $is_wallet,
    'driver_mobile'   =>    $driver_details->mobile_number,
    'driver_avg_ratings'     =>   $driver_details->driver_avg_ratings,
    'distance'   =>   $dis,
    'min_time'   =>   $get_near_car_time
  ];
}

插入表格:

DB::table('bid_request')->insert($data);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章