I want to queue part of a function within my controller, mainly because it accesses a 3rd party API and calculates certain information from said request. I also want to do this to increase my knowledge of queues!
The code which I want queueing is:
The only variables that will need pushing with this if statement
is $postcode
and $clinic ID
(which is figured out above the statement).
if($clinic->postcode != $postcode)
{
$client = new Client([ 'base_uri' => 'https://api.postcodes.io/','timeout' => 2.0, 'verify' => false ]);
$response = $client->get('postcodes/'.$postcode)->getBody();
$input = json_decode($response);
$clinic->latitude = $input->result->latitude;
$clinic->longitude = $input->result->longitude;
$clinic->save();
}
So far I have created the queue
table and migrated it.
I then ran the command: php artisan make:job GetClinicLatAndLongPoints --queued
My question is, how can I put this function inside the GetClinicLatAndLongPoints
including passing the two variables over to do so?
I have so far:
public function handle(Clinic $clinic, $postcode)
{
}
But I'm unsure how to lay things out! Any guidance would be hugely appreciated.
You can pass an instance of your
Clinic
model and the postal code to the constructor of your job, which may look along the lines ofIn your controller you dispatch your job
On a side note: although Laravel comes with the database queue driver using it in production is not a very good idea. Better make use of one of the job queues i.e. beanstalkd.