Laravel Slack webhook Example

Today, I will give you an example of “Laravel Slack Webhook Notification”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

First, what we’re doing here, This is the example :

laravel slack webhook example

send notification in slack using laravel

Use of Laravel Notification

We use different channels for instant notifications to find out bugs or different errors on a local or live server in Laravel, Notification channels help to solve problems instantly for developers.

Sending messages using Incoming Webhooks Laravel

Incoming Webhooks are a simple way to post messages from apps into Slack, Creating an Incoming Webhook gives you a unique URL to which you send a JSON payload with the message text and some options.

We will use the Slack notification channel in Laravel for Error Handling in the given below example:-

Create a Slack app (if you don’t have one already)

Pick a name, choose a workspace to associate your app with (bearing in mind that you’ll probably be posting lots of text messages, so you might want to create a channel for sandbox use), and then click Create App.

Create a Workspace for notification:-

create a workspace in slack

Create a Channel and make it private for our personal use:-

create a channel in slack
workspace and channel created

We successfully created a workspace with a channel, now follow the next step.

Create an App to Generate Notification

hit the given below URL:-

<a href="https://api.slack.com/messaging/webhooks" target="_blank" rel="noreferrer noopener">https://api.slack.com/messaging/webhooks</a>
create a incoming webhook app

Create an app From scratch.

create app

Pick your created workspace to develop your app.

name and workspace setup

Click on Incoming Webhooks

incoming webhook slack in laravel

Activate Incoming Webhooks, after this step clicks on Add New WActivate Incoming Webhook, after this step clicks on Add New Webhook to Workspace.

add new webhook to workspace

Search and select your created channel where you want to get all notifications through your laravel application, now click on Allow button.

select and allow app

After following these steps successfully, we got a new Webhook URL copy this url and paste it on your .env file.

copy slack webhook URL

Paste Webhook URL in .env File

.env

LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T032DQFUN/XXXXXX

Testing a Error Notification (Throw an Error)

app\Exceptions\Handler.php

In the Handler.php file, we trace error, the file name, and line or code.

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Support\Facades\Log;

class Handler extends ExceptionHandler
{
    
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            $errorLog =collect([
                'file'=>$e->getFile(),
                'line'=>$e->getLine(),
                'code'=>$e->getCode(),
            ])->concat(collect($e->getTrace()))->take(5)->toArray();
           Log::channel('slack')->error($e->getMessage(),$errorLog);
        });
    }
}

routes\web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestNotificationExampleController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
#Slack webhook example
Route::get('/contact',[TestNotificationExample::class, 'create']);

app\Http\Controllers\TestNotificationExampleController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Session;
use Illuminate\Support\Facades\Log;

class TestNotificationExampleController extends Controller
{
    public function create(){

        $testerror = $test_undefined_variable_example;
        //return view('contact');
    }
}

Don’t forget to import:-

use Illuminate\Support\Facades\Log;

Run your Application :

http://127.0.0.1:8000/contact

Output :

Now you can see we successfully sent a notification for error message on slack channel.

send an error in slack channel

received a notification in slack channel from laravel
complete log details in slack laravel

In this article, we successfully integrated “Slack notification channel with Laravel using Laravel Slack webhook Example, I hope this article will help you with your Laravel application Project.

Also Read:- How to use the Laravel Debugger Package.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Scroll to Top