Skip to main content

Laravel Queue Example Tutorial

Laravel Queue Example Tutorial

Hi Guys

Today, I will learn how to end email using queue in laravel. we will create email send using queue in this example.We will show how to use queue jobs in laravel from scratch.

When you send email for verification or send invoice then it load time to send mail because it is services. you see some process take time to load like email send, payment gateway etc.If you don't want to wait to user for send email or other process on loading server side process then you can use queue. because it's very fast and visitor will happy to see loading time.

In this example, I will very simple example to create queue with database driver for test email sending.You can definitely understand how to work queue and how it's easy. If you haven't used before then don't worry, here if from starch and very simple.

Here,I give you full example of Queue example step by step like create laravel project,mail,balde,job file etc. So you have to just follow few steps.

Step 1 : Install Laravel Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:


composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Mail Setup

Now this step, We are going from scratch and in first step, we will create email for testing using Laravel Mail facade. So let's simple run bellow command


php artisan make:mail SendEmailTest
After you will have new folder "Mail" in app directory with SendEmailTest.php file. So let's simply copy bellow code and past on that file.
following path:/app/Mail/SendEmailTest.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendEmailTest extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{

}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.test');
}
}

Now we require to create email view using blade file. So we will create simple view file and copy bellow code om following path.

following path:/resources/views/emails/test.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Laravel Queue Example Tutorial - itwebtuts.blogspot.com</title>
</head>
<body>
<center>
<h2>
Laravel Queue Example Tutorial - itwebtuts.blogspot.com
</h2>
</center>
<p>Hi, Sir</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<h5>Thank you Sir. :)</h5>
</body>
</html>
Here configuration of view file, we have to setup for email send, So let' set configuration in .env file:
following path: .env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xyz@gmail.com
MAIL_PASSWORD=123456
MAIL_ENCRYPTION=tls
Step 3: Configuration of Queue

Now In step, we will make configuration on queue driver so first of all, we will set queue driver "database". You can set as you want also we will define driver as redis too. So here define database driver on ".env" file:

following path: .env

QUEUE_CONNECTION=database

After that we need to generate migration and create tables for queue. So let's run bellow command for queue database tables:

Generate Migration:

php artisan queue:table
Run Migration:

php artisan migrate
Step 4: Create Queue Job

Here we will create queue job bey following command, this command will create queue job file with Queueable. So let's run bellow command:


php artisan make:job SendEmailJob

After you have SendEmailJob.php file in "Jobs" directory. So let's see that file and put bellow code on that file.

following path: /app/Jobs/SendEmailJob.php

<?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 App\Mail\SendEmailTest;
use Mail;

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

protected $details;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email = new SendEmailTest();
Mail::to($this->details['email'])->send($email);
}
}
Step 5: Test Queue Job

Here In this step use and test created queue job, so let's simple create route with following code for testing created queue.

following path: /routes/web.php

Route::get('email-test', function(){

$details['email'] = 'your_email@gmail.com';

dispatch(new App\Jobs\SendEmailJob($details));

dd('done');
});

After, you can watch your queue process using laravel queue command, so let's run bellow command:


php artisan queue:listen

Now we are ready to run our example so run bellow command for quick run:


php artisan serve

Now you can open bellow URL on your browser:


http://localhost:8000/email-test

It will help you...

Comments

Popular posts from this blog

Laravel Scout Algolia Full Text Search Example

Hi Guys Today,I will tell how you can full text search utilizing scout algolia. i will show the example of laravel scout algolia full text search.you can full text search utilizing scout algolia api.it can this example full expound scout algolia full text search. I will show the rudimental step of scout algolia api full text search.if you can utilize full text search for install scout and Algolia api package.we are utilizing algolia api utilizing full text search example in laravel. Here the following steps example laravel full text search Using scout algolia Step 1: Create Laravel Project In this step create laravel project following command. composer create-project --prefer-dist laravel/blog Step 2: Database Configuration After create laravel project , we require to make database configuration, you have to add following details on your .env file. 1.Database Username 1.Database Password 1.Database Name In .env file also available host and port details, you can configu

Laravel IP Address Using Get Location Example

Hi Dev, Today,I will learn you how to get location useing ip address in laravel. we will show example of laravel ip address using get location. you can easy to get location useing ip address in laravel.In this example, I will useing stevebauman/location packege get location useing ip address in laravel. Many time you will need to get visitor's details for security, spam prevention etc. It's very easy to get visitor ip address and their location in PHP Laravel. Step 1: Install stevebauman/location Now, We will install stevebauman/location package using below command.Require this package with composer. It is recommended to only require the package for development. composer require stevebauman/location Step 2: Add providers and aliases In this step,We will add below providers and aliases in the "config/app.php" file. config/app.php 'providers' => [ .... Stevebauman\Location\LocationServiceProvider::class, ], 'aliases' => [ .... 'Loca

Laravel 6 validation required if another field is empty

Hii guys, In this artical, i will give you example of laravel 6 in validation required if another field is empty. We know laravel provide several in-built laravel 6 validation required_without . If you need to add validation rules like required if other field is empty in laravel then you can do it using required_without. I am going to explain you, If you can not enter test (field) value at that time test1 (field) is required. So at that time you can add validation required_without. So, you can use this way: "test1" =>"required_without:test" Example: public function store(Request $request) { $request->validate([ "test" =>"required", "test1" =>"required_without:test" ]); dd("Done!"); } If return validation error otherwise show Done!. It will help you...