Jobs in Laravel 10 , Jobs-Queues in Laravel 10

Jobs in Laravel 10 , Jobs-Queues in Laravel 10

17-Apr-2023
| |
Image Carousel

Table of Contents

S.no Contents-topics
1 Introduction of job-queues
2 Controller function for creating data
3 Configuration for job-queues
4 Making job-queues
5 Call the job-queues

1:Introduction of job-queues

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.

Taking a basic example of updating the databae with 100 users , calling from web.php to any controller to perform function.

2:Controller function for creating data

web.php code.  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']);
 

In Controller we are just creation some data to perform jobs in laravel
code:
 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();
      }
     
   
    }
}

 

3:Configuration for job-queues

As 100 reocrds has been created , now we will create jobs table in database which stores jobs and process them one by one with FCFS( first come first serve).
command for table :Copy

php artisan queue:table

It will create migration file and then we have to create this migration in our database
command for migartion
  Copy

php artisan migrate

4:Making job-queues

 This will create Jobs table in database
Now we will create our new job with name  
UpdateUserData
command for job: Copy

php artisan make:job UpdateUserData

 

Now for the testing the jobs in laravel we will change the name of the users in App\Jobs\UpdateUserData with the help of jobs , paste the following 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 make QUEUE_CONNECTION=sync to QUEUE_CONNECTION=database
.env 
 Copy

QUEUE_CONNECTION=database

Now from controller we call this UpdateUserData job by using UpdateUserData::dispatch()  method 
code: Copy

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

Now you will see an entry in jobs table.

 

5:Calling the job-queues

Now hit following command in your terminal 
command:
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
command for restart:
 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 &

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

Tags: create jobs in laravel 10, jobs-queues in laravel 10 , create jobs-queues in laravel 10 , queueable in laravel 10,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More

Newsletter