Laravel + Twilio SMS System Integration in Simple Way
Categories - Laravel PHP Framework Tags - PHP Laravel   Maniruzzaman Akash   8 months ago   761   0

Laravel + Twilio SMS System Integration in Simple Way

Today, we'll integrate laravel and Twilio SMS system to send SMS from Twilio sms number to another number.

 

Step 1:

Create Laravel Project called sms-portal -

composer create-project laravel/laravel sms-portal

 

Step 2:

Setup database in .env file - Create a database called - sms_portal in your MySQL client.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sms_portal
DB_USERNAME=root
DB_PASSWORD=12345678

 

Step 3:

Run Migration and Start application -

php artisan migrate
php artisan serve

 

Step 4:

Install and Setup Twilio

Run this command to install twilio PHP sdk.

composer require twilio/sdk

 

Get twilio credential

from https://console.twilio.com

Screenshot-2022-03-19-at-2-41-39-PM

 

Set this twilio data in .env file -

TWILIO_SID="ACxxxxxxxxxxxxxxx"
TWILIO_AUTH_TOKEN="fexxxxxxxxxxxxxxx"
TWILIO_NUMBER="+1xxxxxxxxx"

 

Step 4:

Create Our Model and Migration to store phone numbers -

php artisan make:model UserPhone -m

It'll create two file - App\Models\UserPhone.php and databases/migrations/2022_03_19_063150_create_user_phones_table.php

where date time is dynamic for that time in migration file.

Migration File - User Phone Migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUserPhonesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_phones', function (Blueprint $table) {
            $table->id();
            $table->string('phone_number');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_phones');
    }
}

 

Model File - User Phone Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserPhone extends Model
{
    use HasFactory;

    protected $table = 'user_phones';

    protected $fillable = [
        'phone_number'
    ];
}

 

 

Create a Controller - HomeController by following command

php artisan make:controller HomeController

HomeController: 

 

Step 6:

Web.php File to Add Routes:

 

Step 7:

Finally Add Our View File - in welcome.blade.php

 

Final Output in http://localhost:8000

Screenshot-2022-03-19-at-3-02-36-PM

Previous
PHP If-else-elseif and Switch-case
Next
PHP String Functions - All necessary String functions in PHP to manage strings better.