I am doing a api for cab booking app like uber and ola using laravel.In that the customer will send the request to available cab driver using gcm and cab driver will accept the request.During these process when customer click the request button it will be loading until driver accept the request.When driver accept the request customer get the driver details and driver get the customer details.How can i do it in laravel..??
I can send the customer request using below code:
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 = 'http://ift.tt/YbbTRA';
$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);
}
}
I am using Helper class to write the codeing for waiting until the driver accept the request:
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;
}
}
The helper class do the work of get the driver id when they accept the request within 15 seconds.It's work for me but the driver will accept the request within 15 seconds.How can i do without time duration to check the db if driver id present or not and also it will loading in customer side until driver accept the request.!!
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/24upWr6
via IFTTT
Aucun commentaire:
Enregistrer un commentaire