DEV Community

Cover image for Laravel + Twilio Integration - The Easy Way
Maniruzzaman Akash
Maniruzzaman Akash

Posted on

Laravel + Twilio Integration - The Easy Way

Hi guys,
Let's Integrate Twilio with our Laravel application easily with some few steps.

Step-1: Create a Laravel Project called - sms-portal.

composer create-project laravel/laravel sms-portal
Enter fullscreen mode Exit fullscreen mode

Step-2: Configure .env file - After creating the database sms_portal

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sms_portal
DB_USERNAME=root
DB_PASSWORD=12345678
Enter fullscreen mode Exit fullscreen mode

Step-3: Start Server

php artisan migrate
php artisan serve
Enter fullscreen mode Exit fullscreen mode

Step-4: Install Twilio PHP SDK

composer require twilio/sdk
Enter fullscreen mode Exit fullscreen mode

Step-5: Get Twilio credential
Go to https://console.twilio.com

Console of Twilio Page

Check their your Account SID, Auth Token and Twiolio Phone Number.

Set this value in .env File -

TWILIO_SID="ACxxxxxxxxxxxxxxx"
TWILIO_AUTH_TOKEN="fexxxxxxxxxxxxxxx"
TWILIO_NUMBER="+1xxxxxxxxx"
Enter fullscreen mode Exit fullscreen mode

Step-6: Create Our UserPhone model
Using this model we'll send message to these phone number from our registered Twilio Phone number.

php artisan make:model UserPhone -m
Enter fullscreen mode Exit fullscreen mode

In CreateUserPhonesTable migration - only add the phone_number string column.

<?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');
    }
}
Enter fullscreen mode Exit fullscreen mode

In UserPhone 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'
    ];
}
Enter fullscreen mode Exit fullscreen mode

For More, Please Read Full Article in DevsEnv - https://devsenv.com/tutorials/laravel-+-twilio-sms-system-integration-in-simple-way

Our Final application will be look like this -

Final Demo of Laravel Twilio Application

Top comments (0)