Simple usage of laravel queue

1. The queue configuration file is stored in Configure config/queue.phpaccording to your own situation

2..env file QUEUE_DRIVER=database (configured according to personal situation, redis, etc.)

3. Create a jobs table (you don't need to build a table if you don't need a database)

php artisan queue:table
php artisan migrate 

4. Create a task file
php artisan make:job Testqueue 
will generate the directory and file app\Jobs\Testqueue.php
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;

class testqueue 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()
    {
        //
         DB::table('tests')->insert(['name'=>'test name' ]);//I write my own code here
    }

5. Build your own controller

write code inside

    public function login(Request $request){
        $this->dispatch(new testqueue());

 After accessing this controller, there will be a queue record in jobs

start queue

Php artisan queue:work 
will delete the data in the jobs table and add a record in the tests table

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970554&siteId=291194637