In this tutorial you will learn about the Laravel 8 Send SMS Notification to Mobile/ Phone Example and its application with practical example.
In this Laravel 8 Send SMS Notification to Mobile/ Phone Example tutorial I will show you how to send SMS notification to mobile in laravel 8 application. In this tutorial you will learn to send sms notification to mobile in laravel. In this example we will be using nexmo sms package to send sms in laravel 8 application.
Laravel 8 Send SMS Notification to Mobile/ Phone Example
In this step by step tutorial I will demonstrate you with example how to send sms notification in laravel 8 application using nexmo sms package. Please follow instruction given below:
- Step 1 – Download Laravel 8 Application
- Step 2 – Connecting App to Database
- Step 3 – Install SMS Package
- Step 4 – Create Route
- Step 5 – Create Controller By Artisan Command
- Step 6 – Run Development Server
Step 1 – Download Laravel 8 Application
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database-name DB_USERNAME=database-user-name DB_PASSWORD=database-password |
Step 3 – Install SMS Pakcage
In this step, we will install nexmo sms Package via the composer dependency manager. Use the following command to install nexmo sms Package.
1 |
composer require nexmo/client |
Visit the link given below and create a nexmo account there:
1 |
https://www.nexmo.com |
After creating account you will be provided app id and secret key.
Step 4 – Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 |
use App\Http\Controllers\SendSMSController; Route::get('send-sms', [SendSMSController::class, 'index']); |
Step 5 – Create Controller By Artisan Command
Now, lets create a controller named SendSMSController using command given below –
1 |
php artisan make:controller SendSMSController |
Now, go to app/http/controllers and open SendSMSController.php file. And put the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SendSMSController extends Controller { public function index() { $basic = new \Nexmo\Client\Credentials\Basic('key', 'secret'); $client = new \Nexmo\Client($basic); $message = $client->message()->send([ 'to' => '9181600*****', 'from' => 'Nexmo', 'text' => 'A text message sent using the Nexmo SMS API' ]); dd('message send successfully'); } } |
Step 6 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/send-sms |