Jobs in Laravel 9 , Jobs-Queues in Laravel 9

Jobs in Laravel 9 , Jobs-Queues in Laravel 9

15-Sep-2022
| |
Image Carousel

Jobs-queues is the one of best feature of laravel , to do work in the background while performing front actions, like if you have 100 emails to hit  in the morning and performing particular functions , jobs-queues is the best optin to perform this action. It actually create a jobs table in database and work accordingly.Like wise if you have some payment payout system in your system which work on some cron job then job-queues is the option to perform such actions.

I am taking a basic example of updating the databae with 100 users.

Calling my controller function with createDeveloper route .  Copy

<?php
 
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DeveloperCorner;
use App\Models\Role;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
Route::get('/', function () {
    return view('welcome');
});
Route::get('/createDeveloper',[DeveloperCorner::class,'createDeveloper']);
 

So firstly i creted some dummy data in my database  from Controller like it creates 100 records in database Copy

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Models\User;
 
use Hash;
 
use Auth;
class DeveloperCorner extends Controller
{
public function createDeveloper()
    {
      for($i=0;$i<100;$i++)
      {
        $user         =  new User();
        $user->name   =  'Developer Corner';
        $user->email   =  'developerCorner'.$i.'@yopmail.com';
        $user->password = Hash::make('1234');
        $user->save();
      }
     
   
    }
}

 

As 100 reocrds has been created , now we will create jobs table in database .Copy

php artisan queue:table

After that  Copy

php artisan migrate

 This will create Jobs table in database
Now we will create our job UpdateUserData Copy

php artisan make:job UpdateUserData

 

Paste the follwing code in app/Jobs/UpdateUserData to update the user data and see the changes Copy

<?php
 
namespace App\Jobs;
 
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
 
class UpdateUserData implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
 
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $users = User::get();
        $i = 1;
        foreach($users as $list){  
     
            User::where('id',$list->id)->Update(['name'=>'developerCorner'.$i.'']);
            $i++;
        }
    }
}

 in .env file mke QUEUE_CONNECTION=sync to QUEUE_CONNECTION=database Copy

QUEUE_CONNECTION=database

Now from controller we call this UpdateUserData job  Copy

  use App\Jobs\UpdateUserData;
   public function UpdateUserData()
    {
      UpdateUserData::dispatch();
    }

 

Now you will see an entry in jobs table.

 

Now hit following command in your terminal Copy

 php artisan queue:work

This will take the data from jobs database and perform the corresponding functions in a queue.
If you change the functionality of your job then hit this command Copy 

php artisan queue:restart

again   php artisan queue:work

if you want to make terminal work in the backgroud after you closes your console or terminal use this Copy

 nohup php artisan queue:work &

Note : On live servers sometimes it automatically stops due to server reboot then use cronjob and call the artisan from url .

Route::get('jobsWork', function () { Artisan::call('queue:work'); }); 

Tags: create jobs in laravel 9, jobs-queues in laravel 9 , create jobs-queues in laravel 9 , queueable in laravel 9,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
Related Articles
Load More

Newsletter