@NabeelHassan I see now.
You can simplify this code like
$provider = Provider::doesnthave('incoming_requests')
->whereNotIn('id', $occupiedProviders)
->with('service')
->select(DB::Raw("(6371 * acos( cos( radians('$latitude') ) * cos( radians(latitude) ) * cos( radians(longitude) - radians('$longitude') ) + sin( radians('$latitude') ) * sin( radians(latitude) ) ) ) AS distance"), 'id')
->where('status', 'approved')
->whereRaw("(6371 * acos( cos( radians('$latitude') ) * cos( radians(latitude) ) * cos( radians(longitude) - radians('$longitude') ) + sin( radians('$latitude') ) * sin( radians(latitude) ) ) ) <= $distance")
->whereHas('service', function ($query) use ($service_type) {
$query->where('status', 'active');
$query->where('service_type_id', $service_type);
})
->orderBy('distance', 'asc')
->first();
if ($provider) {
Redis::sadd('occupied_providers', $provider->id);
}
notes
- use first instead of get()
- use singular $provider now that it is a first() method
- use camel case for variable names (eg $provider not $Provider)
Also, you need to handle the case of no results.
Then, in your finally block
} finally {
if ($lock) {
// Cache::lock($lockName)->release();
// Cache::lock($lockName)->release();
return response()->json(Cache::lock($lockName)->release());
return response()->json(Cache::lock($lockName)->forceRelease());
}
}
According to the php documentation I posted first, Your should not return ANYTHING in the finally section, since;
Additionally, if the finally block also contains a return statement, the value from the finally block is returned.
So you will not get any output from your function other than the result of cache lock release.