Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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 :
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.
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:-
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 Channel and make it private for our personal use:-
We successfully created a workspace with a channel, now follow the next step.
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 an app From scratch.
Pick your created workspace to develop your app.
Click on Incoming Webhooks
Activate Incoming Webhooks, after this step clicks on Add New WActivate Incoming Webhook, after this step clicks on 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.
After following these steps successfully, we got a new Webhook URL copy this url and paste it on your .env file.
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.
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.