Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Each Loop in Laravel

I have below database structure in my driver_location table

enter image description here

From this table I am fetching near-by driver id who are within 5 km distance and status equal online with below query in laravel

 $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);

Now what I want to do is for each near by user_id from driver_location table a request will be inserted in bid_request table limited to 5 nearby drivers. I have tried with a for loop like below but it is inserting record for same user_id from driver location table multiple times. I need one request for each user

 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'
like image 439
Mithu Avatar asked Oct 22 '25 18:10

Mithu


1 Answers

If you want to insert 5 record in bid_request table than get only 5 record from below query:

$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();

Now you need to array of records to insert in table :

$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
  ];
}

Insert in table :

DB::table('bid_request')->insert($data);
like image 58
Yasin Patel Avatar answered Oct 24 '25 09:10

Yasin Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!