Knowledge Base

Empty filter

Laravel Queue job

Create a Job
php artisan make:job UpdateContentJob

Modify the Job Class (app/Jobs/UpdateContentJob.php)
Update it to process each record:

php
namespace App\Jobs;

use App\Models\YourModel;
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;

class UpdateContentJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct()
{
//
}

public function handle()
{
YourModel::chunk(100, function ($records) {
foreach ($records as $record) {
$record->content = preg_replace('/(<a href="https?:\/\/)/', "\n\n$1", $record->content);
$record->save();
}
});
}
}

Dispatch the Job on Button Click
In your controller:

php
use App\Jobs\UpdateContentJob;

public function updateAllRecords()
{
dispatch(new UpdateContentJob());
return back()->with('success', 'Update has been queued.');
}

Configure the Queue Driver
In .env, set:

QUEUE_CONNECTION=database

php artisan queue:table
php artisan migrate

Start the Queue Worker
php artisan queue:work

Published
Back to Index