Laravel 8 send sms using nexmo example; In this tutorial, you will learn how to send SMS notification to mobile using nexmo in laravel 8 app.
How to Send SMS/Messages to Phone/Mobile in Laravel 8
- 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 download or install laravel 8 new setup. So, open terminal and type the following command to install new laravel 8 app into your machine:
composer create-project --prefer-dist laravel/laravel blog
Step 2 – Connecting App to Database
In this step, setup database with your downloded/installed laravel 8 app. So, you need to find .env file and setup database details as following:
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, open again your command prompt and execute the following command on it. To install nexmo sms package:
composer require nexmo/client
Then visit nexmo website and create nexmo account from here : https://www.nexmo.com. When you will be done account creation. This will give you the app id and secret key.
Step 4 – Create Routes
In this step, open web.php file from routes direcotry. And update the following routes into web.php file:
use App\Http\Controllers\SendSMSController; Route::get('send-sms', [SendSMSController::class, 'index']);
Step 5 – Create Controller By Artisan Command
In this step, run the following command on command prompt to create controller file:
php artisan make:controller SendSMSController
After that, go to app/http/controllers and open SendSMSController.php file. And update the following code into it:
<?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
Finally, open command prompt and run the following command to start developement server:
php artisan serve
Then open your browser and hit the following url on it:
http://127.0.0.1:8000/send-sms