DEV Community

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

Posted on

3 2

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay