【问题标题】:How to get the id automatically in db如何在db中自动获取id
【发布时间】:2016-05-05 15:42:55
【问题描述】:

我正在使用 laravel 为 uber 和 ola 等出租车预订应用程序做一个 api。客户将使用 gcm 将请求发送给可用的出租车司机,出租车司机将接受该请求。在这些过程中,当客户单击请求按钮时它将加载直到驱动程序接受请求。当驱动程序接受请求时,客户获取驱动程序详细信息,驱动程序获取客户详细信息。我如何在 laravel 中做到这一点..??

我可以使用以下代码发送客户请求:

public function customer_booking(Request $req)
{
    if ($req->isMethod('post'))
    {
        $customer_id=$req->customer_id;
        $driver_type=$req->type_id;
        //getting driver 
        $res=DriverLatLongModel::where('driver_type',$driver_type)
           ->where('booking_status','3')->orwhere('booking_status','2')
           ->where('active_status','0')->orderBy('created_at', 'desc')->first();
        $driver_lat='';
        $driver_long='';
        $driver_id_booking='';
        if(empty($res))
        {
            $gcm_data[]=array('status'=>'0');
             return Response::json(array('message'=>$gcm_data), 200);

        }
        else
        {
            $driver_id_booking=$res->driver_id;
        }
            $registration_id = array();
            //getting gcm id
            $driver_position = DriverLatLongModel::where('driver_id',$driver_id_booking)->get();
                foreach($driver_position as $resid)
                {
                    array_push($registration_id , $resid['registration_id']);
                }

        //send gcm 
        $url = 'https://android.googleapis.com/gcm/send';
        $message_gcm = array("Status"=>"1","Notice" =>"WELCOME","customer_id"=>$customer_id);
         $fields = array(
             'registration_ids' => $registration_id ,
             'data' => $message_gcm ,
         );
         $headers = array(
             'Authorization: key=AIzaSyDCDmsrv3ELqD_6qseFgERciRnmm9uBtNg',
             'Content-Type: application/json'
         );
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
         $result = curl_exec($ch);
         if ($result === FALSE) {
             die('Curl failed: ' . curl_error($ch));
         }
         curl_close($ch);
         $driver_data=array();
         //getting result of driver side
         $finding_donor_loc=CustomerBookingModel::getCustomer($customer_id);
         if(empty($finding_donor_loc))
         {
            $driver_data[]=array('status'=>'0');
         }
         else
         {
            //getting driver details
            $driver_details=DriverDetailsModel::where('driver_id',$finding_donor_loc)->first();
            $driver_name=$driver_details->driver_name;
            $driver_data[]=array('status'=>'1','driver_name'=>$driver_name);
         }
         return Response::json(array('message'=>$driver_data), 200);
    }

}

我正在使用 Helper 类编写等待驱动程序接受请求的代码:

public static function getCustomer($customer_id)  
      { 
            $duration =15; // Duration of the loop in seconds
            $sleep = 5;// Sleep beetween each execution (with stuff execution)
            for ($i = 0; $i < floor($duration / $sleep); ++$i)
            {
                $start = microtime(true);
                 $users = DB::table('booking_information')
                    ->where('customer_id',$customer_id)->where('booking_status','1')
                    ->where('book_confirm','1')->orderBy('created_at', 'asc')->first();
                time_sleep_until($start + $sleep);
            }   
          if(empty($users))
        {
            $confim_driver_id='0';
            return $confim_driver_id;
        }
        else
        {
            $confim_driver_id=$users ->driver_id;
             return $confim_driver_id;
         }

    }

当他们在 15 秒内接受请求时,助手类会完成获取驱动程序 ID 的工作。这对我有用,但驱动程序将在 15 秒内接受请求。如果没有持续时间来检查数据库,我该怎么做驱动程序 ID 存在与否,它也会在客户端加载,直到驱动程序接受请求。!!

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    如果您想在预订确认后立即将预订确认返回给用户,那么您可以在 for 循环中放置一个 IF 条件,并在满足条件后立即返回 driver_id。

        $duration   =   15; // Duration of the loop in seconds
        $sleep      =   5;  // Sleep between each execution (with stuff execution)
    
        for ($i = 0; $i < floor($duration / $sleep); ++$i)
        {
    
            $start = microtime(true);
    
            // query the database to check if the booking is confirmed
            $booking = DB::table('booking_information')
             ->where('customer_id', $customer_id)
             ->where('booking_status', '1')
             ->where('book_confirm', '1')
             ->orderBy('created_at', 'asc')
             ->first();
    
            // if the booking is confirmed then return the driver_id
            // return will also stop the loop
            if($booking) {
                return $booking->driver_id;
            }
    
            // Make the script execution sleep 
            time_sleep_until($start + $sleep);
    
        }   
    
        // if booking is not confirmed then return 0
        if(empty($booking)) return 0;
    

    在 laravel 中执行此类任务的更好方法可以通过事件和侦听器来实现。工作流程是这样的。

    1. Your API recieves a request to book a cab
    2. A NewBookingRequest event is fired & NewBookingRequestListener sends a push notificaton to the nearest driver about the new booking
    3. Driver confirms/denies the booking & your API fires BookingConfirmed/BookingDenied event
    4. Notification is sent to the user about the booking
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2016-03-16
      • 2011-10-09
      相关资源
      最近更新 更多