<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Menebraye</title>
    <description>The latest articles on DEV Community by Michael Menebraye (@hummingbed).</description>
    <link>https://dev.to/hummingbed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F703308%2F848e5f14-4bee-4f30-8948-6d86abfffe63.jpeg</url>
      <title>DEV Community: Michael Menebraye</title>
      <link>https://dev.to/hummingbed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hummingbed"/>
    <language>en</language>
    <item>
      <title>Practical Guide to Cleaner Laravel Controllers: Harnessing the Power of Accessors and Mutators</title>
      <dc:creator>Michael Menebraye</dc:creator>
      <pubDate>Sun, 21 Jan 2024 15:36:51 +0000</pubDate>
      <link>https://dev.to/hummingbed/practical-guide-to-cleaner-laravel-controllers-harnessing-the-power-of-accessors-and-mutators-3ejh</link>
      <guid>https://dev.to/hummingbed/practical-guide-to-cleaner-laravel-controllers-harnessing-the-power-of-accessors-and-mutators-3ejh</guid>
      <description>&lt;p&gt;Laravel uses a Model-View-Controller architectural pattern and controllers play an important role. The main task of a controller in Laravel is to manage user http requests, render views and communicate with the application models for data modifications. When building any Laravel application, it is essential to adhere to best practices by implementing the MVC design pattern. Following this pattern ensures the maintainability and reusability of your code. For instance, the controller can be employed for writing business logic; however, this is generally not considered a best practice. Following best practices involves keeping controllers focused on handling http request and application flow, while business logic is better placed in the model or service for a cleaner and more organized codebase. Now, let's explore an actual case that demonstrates the functioning of controllers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Illustration of a Controller in Action
&lt;/h2&gt;

&lt;p&gt;In the accounting industry, a Corporate Controller holds a pivotal managerial role, overseeing financial functions within an accounting firm. This position entails the management of departments like Financial Reporting, Budgeting and Forecasting, Compliance, and Treasury Management. The Corporate Controller is tasked with ensuring precise financial reporting, upholding internal controls, adhering to regulations, making strategic financial decisions, and providing leadership and supervision to teams within the finance division.&lt;/p&gt;

&lt;p&gt;The role of a Corporate Controller in the accounting industry can be compared with that of a Laravel Controller. The controller in Laravel is an important part that controls the flow and request handling inside the application. It acts as a hub through which different functions are managed analogous to how the Corporate Controller manages financial aspects.&lt;/p&gt;

&lt;p&gt;For example, while a Corporate Controller oversees departments such as Financial Reporting, Budgeting and Forecasting, and Compliance, a Laravel Controller handles different aspects of an application, including data retrieval and processing views rendering. The Laravel Controller, like its corporate counterpart, acts as a key guardian, ensuring data accuracy. Given the facts we've emphasized about maintaining a tidy controller, how can we ensure the cleanliness of our controllers? This can be achieved through the use of accessors and mutators.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessors
&lt;/h2&gt;

&lt;p&gt;Accessors are also used to create fake attributes that do not belong to the database column. These fake attributes can be accessed as if they are stored columns in the database. Data obtained from the database can be manipulated using accessors. In object-oriented programming (OOP) in any language, accessors bear similarity to getters. Let us take a look at some possible situations where assessors can be applied to our applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 1:
&lt;/h2&gt;

&lt;p&gt;Picture yourself working on a user registration system, where the goal is to showcase each user's full name in a specific format without persistently storing it in the database. To accomplish this, you can employ an accessor to create a fullname attribute by combining the first and last names, capitalizing the initial letter of each name. This fullname attribute exists only in memory and is not in the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 2:
&lt;/h2&gt;

&lt;p&gt;You are building an API for frontend developers, and you want to display user information without exposing the exact user ID for security reasons. You can use accessors to hash the user ID before sending it to the front-end. &lt;/p&gt;

&lt;h2&gt;
  
  
  Naming conventions for accessors:
&lt;/h2&gt;

&lt;p&gt;Accessors follow a naming convention that requires the creation of methods in the model to format attribute values upon retrieval from the database. This convention dictates using the &lt;strong&gt;get{AttributeName}Attribute&lt;/strong&gt; method on the model, where {&lt;strong&gt;AttributeName&lt;/strong&gt;} represents the name of the target attribute. For instance, if dealing with an attribute named &lt;strong&gt;first_name&lt;/strong&gt;, the corresponding accessor method would be &lt;strong&gt;getFirstNameAttribute&lt;/strong&gt;. Please note that attributes of a model are the fields or columns in the associated database table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgd3jlbz1g8wrcgr3hdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgd3jlbz1g8wrcgr3hdl.png" alt="accessors Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mutators
&lt;/h2&gt;

&lt;p&gt;Like accessors, mutators are also used for data manipulation. The main difference with Mutators is that the manipulated data is stored in a database. They can be compared to setters in object-oriented programming (OOP). We are going to consider some possible scenarios where mutators can be implemented.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 1:
&lt;/h2&gt;

&lt;p&gt;Picture yourself developing a registration functionality for a web application, and your goal is to guarantee the security of user credentials by encrypting the password. You can use a mutator to hash the password before saving it to the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 2:
&lt;/h2&gt;

&lt;p&gt;Imagine you are building a system where users are allowed to save a phone number containing a plus symbol (+) and you want to remove the plus symbol before storing the number in the database, you can use a mutator to implement this feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  Naming conventions for mutators:
&lt;/h2&gt;

&lt;p&gt;Mutators adhere to a naming convention that entails generating methods in the model to modify the attributes before they are stored in the database. The convention specifies using the &lt;strong&gt;set{AttributeName}Attribute&lt;/strong&gt; method on the model, where &lt;strong&gt;{AttributeName}&lt;/strong&gt; corresponds to the name of the targeted attribute. For instance, with an attribute named &lt;strong&gt;first_name&lt;/strong&gt;, the mutator method would be &lt;strong&gt;setFirstNameAttribute&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbczbe3i54z1crw2swrz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frbczbe3i54z1crw2swrz.png" alt="mutators Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reasons for using accessors and mutators.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Maintenance&lt;/strong&gt;: They make it easier to modify the behavior of certain attributes in one centralized location, reducing the risk of introducing errors while making updates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability&lt;/strong&gt;: Separating logic related to attribute manipulation from the main application logic improves the readability of the code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reusability&lt;/strong&gt;: By encapsulating logic within them, you can reuse these methods across different parts of your application, promoting code reuse and avoiding duplicating similar logic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Testing&lt;/strong&gt;: They make it easier to unit test specific behavior associated with attribute retrieval or setting.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Basic knowledge of getters and setters in object-oriented programming (OOP)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Laravel and PHP Basics&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mysql Database&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Composer and PHP installed&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementing Accessors and Mutators
&lt;/h2&gt;

&lt;p&gt;Now that you have a solid understanding of accessors and mutators in Laravel, let's dive into some practical scenarios where these concepts can be implemented. We will explore step-by-step guides for implementing the few scenarios mentioned earlier:&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Creating and setting up a new &lt;strong&gt;&lt;a href="https://laravel.com/docs/10.x/installation#creating-a-laravel-project" rel="noopener noreferrer"&gt;laravel application&lt;/a&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In your terminal, execute the following command to generate a new Laravel Project named &lt;strong&gt;example-app&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer create-project laravel/laravel example-app


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, open the example-app project and locate the &lt;strong&gt;.env&lt;/strong&gt; file to set up your database preferences. Input the desired database name in the &lt;strong&gt;DB_DATABASE&lt;/strong&gt; field and, if applicable, enter a password in the &lt;strong&gt;DB_PASSWORD&lt;/strong&gt; field.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=*****
DB_USERNAME=root
DB_PASSWORD=****


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Go to the database directory, then navigate to the migrations folder, find the migration file suffix named &lt;strong&gt;_create_users_table.php&lt;/strong&gt;, and open it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7tb288c6tb8sf3gh0864.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7tb288c6tb8sf3gh0864.gif" alt="users_table Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, replace the &lt;strong&gt;up&lt;/strong&gt; method with the code below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public function up(): void
{
    Schema::create('users', function (Blueprint $table) {
        $table-&amp;gt;id();
        $table-&amp;gt;string('first_name');
        $table-&amp;gt;string('last_name');
        $table-&amp;gt;string('email')-&amp;gt;unique();
        $table-&amp;gt;timestamp('email_verified_at')-&amp;gt;nullable();
        $table-&amp;gt;string('password');
        $table-&amp;gt;rememberToken();
        $table-&amp;gt;timestamps();
    });
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then run this command on your terminal&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan migrate


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should have these empty rows in your users table after running the migration command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xddwy2ncqtip8f8wljo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xddwy2ncqtip8f8wljo.png" alt="users table migration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Implementing Mutators
&lt;/h2&gt;

&lt;p&gt;Go to  &lt;strong&gt;app/models/&lt;/strong&gt; directory and locate the &lt;strong&gt;User.php&lt;/strong&gt; model file. This is the location where we will implement all our business logic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1148rpmif1fam7vkyuw2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1148rpmif1fam7vkyuw2.gif" alt="Implementing Mutators Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Replace the existing code in this file with the provided code below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
   protected $fillable = [
       'first_name',
       'last_name',
       'email',
       'password',
   ];
   protected $hidden = [
       'password',
       'remember_token',
   ];

   public function setPasswordAttribute($value)
   {
       $this-&amp;gt;attributes['password'] = bcrypt($value);
   }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code illustrates the creation of a fillable property, hidden property, and a mutator method.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;$fillable&lt;/strong&gt; property permits mass assignment solely for specified fields.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;$hidden&lt;/strong&gt; property safeguards sensitive data by omitting it from public representations when the model is converted to an array or JSON.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;setPasswordAttribute&lt;/strong&gt; method  hashes user passwords before they are stored in the database.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our primary focus is on the &lt;strong&gt;setPasswordAttribute&lt;/strong&gt; method. We use Laravel's &lt;strong&gt;bcrypt&lt;/strong&gt; function to encrypt the provided password value, setting the password attribute to the hashed password value before storing it in the database.&lt;/p&gt;

&lt;p&gt;Create a user controller by executing this command on your terminal:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan make:controller UserController


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Navigate to the &lt;strong&gt;app/Http/Controllers/&lt;/strong&gt; directory and open the UserController file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy56c2zd2fp5ms7sl5llu.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy56c2zd2fp5ms7sl5llu.gif" alt="Controllers directory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Replace the existing code in the UserController file with the provided code below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
  public function showCreateUserForm()
  {
      return view('create-user');
  }

  public function createUser(Request $request)
  {
      $user = User::create([
          'first_name' =&amp;gt; $request-&amp;gt;input('first_name'),
          'last_name' =&amp;gt; $request-&amp;gt;input('last_name'),
          'email' =&amp;gt; $request-&amp;gt;input('email'),
          'password' =&amp;gt; $request-&amp;gt;input('password'),
      ]);

      return redirect('/create-user')-&amp;gt;with('success', 'User created successfully');
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Go to &lt;strong&gt;resources/views/&lt;/strong&gt; directory and create a new file named &lt;strong&gt;create-user.blade.php&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjkx3t7mv0dnzgl9sieqa.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjkx3t7mv0dnzgl9sieqa.gif" alt="create-user Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy and Paste the code provided below in create-user.blade.php&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@if(session('success'))
  &amp;lt;div style="color:green;text-align:center;"&amp;gt;
      {{ session('success') }}
  &amp;lt;/div&amp;gt;
@endif

&amp;lt;form method="POST" action="{{ url('/create-user') }}"&amp;gt;
  @csrf

  &amp;lt;label for="first name"&amp;gt;First Name:&amp;lt;/label&amp;gt;
  &amp;lt;input type="text" name="first_name" required&amp;gt;
  &amp;lt;br&amp;gt;

  &amp;lt;label for="last name"&amp;gt;Last Name:&amp;lt;/label&amp;gt;
  &amp;lt;input type="text" name="last_name" required&amp;gt;
  &amp;lt;br&amp;gt;

  &amp;lt;label for="email"&amp;gt;Email:&amp;lt;/label&amp;gt;
  &amp;lt;input type="email" name="email" required&amp;gt;
  &amp;lt;br&amp;gt;

  &amp;lt;label for="password"&amp;gt;Password:&amp;lt;/label&amp;gt;
  &amp;lt;input type="password" name="password" required&amp;gt;
  &amp;lt;br&amp;gt;

  &amp;lt;button type="submit"&amp;gt;Create User&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Open the &lt;strong&gt;web.php&lt;/strong&gt; in the routes directory and replace the existing code with the code provided below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

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

Route::get('/', function () {
  return view('welcome');
});

Route::get('/create-user', [UserController::class, 'showCreateUserForm']);
Route::post('/create-user', [UserController::class, 'createUser']);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Execute the command below to serve the application&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan serve


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, copy the base URL from the terminal and paste it into your browser. Then append the &lt;strong&gt;/create-user&lt;/strong&gt; route to the base URL. Your view should resemble this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8w8kl6jetqoadqu9iso4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8w8kl6jetqoadqu9iso4.png" alt="localhost"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a new user by entering the necessary information into the input fields. Keep in mind that no validation has been implemented in this application, so ensure accurate information is provided.&lt;/p&gt;

&lt;p&gt;Open your database management tool and check the user table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxulrsbtb3pzysr299vho.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxulrsbtb3pzysr299vho.png" alt="DB Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can observe that the user password in the password column has been hashed. It's worth noting that we chose not to implement password encryption in the controller, which is a common practice among developers. This method of using a mutator to hash the password helps maintain a clean and straightforward controller.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Implementing Accessors
&lt;/h2&gt;

&lt;p&gt;We won't create a new Laravel application to implement accessors; instead, we'll utilize the existing &lt;strong&gt;example-app&lt;/strong&gt; used for implementing mutators. Let's proceed with implementing the accessors method. In this instance, we will create a username using the first name, last name, and id attribute.&lt;/p&gt;

&lt;p&gt;Copy the code provided below and paste it into the user model, positioning the:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;getUsernameAttribute&lt;/strong&gt;: below the &lt;strong&gt;setPasswordAttribute&lt;/strong&gt; method.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;$appends:&lt;/strong&gt; below or top of the protected properties.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;$appends&lt;/strong&gt; is used to add extra information to the JSON output of your model without having to modify the database schema. It's useful for including manipulated or formatted data that you want to send to your frontend or other parts of your application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

protected $appends = ['username'];

public function getUsernameAttribute()
{
   $firstName = $this-&amp;gt;attributes['first_name'];
   $lastName = $this-&amp;gt;attributes['last_name'];
   $id = $this-&amp;gt;attributes['id'];
   $username = $firstName . '-' . $lastName . '-' . $id;

   return strtolower($username);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, navigate to the &lt;strong&gt;UserController&lt;/strong&gt;, then copy the code provided below and paste the code below the &lt;strong&gt;CreateUser&lt;/strong&gt; method. This method will retrieve all the users stored in the database.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public function getUserName()
{
   $users = User::all();
   return view('create-user', ['users' =&amp;gt; $users]);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add the router method below in the &lt;strong&gt;web.php&lt;/strong&gt; file located in the routes directory.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Route::get('/create-user', [UserController::class, getUserName]);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Finally, copy the provided code below and paste it in the &lt;strong&gt;create-user.blade.php&lt;/strong&gt; file located in the &lt;strong&gt;resources/views/&lt;/strong&gt; directory. Make sure to insert it below the closing HTML form tag.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;h1&amp;gt;All Users&amp;lt;/h1&amp;gt;

&amp;lt;ul&amp;gt;
  @foreach ($users as $user)
      &amp;lt;li&amp;gt; &amp;lt;b&amp;gt;First Name:&amp;lt;/b&amp;gt; {{ $user-&amp;gt;first_name }} &amp;lt;br&amp;gt;
      &amp;lt;b&amp;gt;Last Name:&amp;lt;/b&amp;gt; {{ $user-&amp;gt;last_name }} &amp;lt;br&amp;gt;
      &amp;lt;b&amp;gt;User Name:&amp;lt;/b&amp;gt; {{ $user-&amp;gt;username }}&amp;lt;/li&amp;gt;
  @endforeach
&amp;lt;/ul&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Go to your browser and refresh the page; you should observe that a new username that is not stored in the database is being displayed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff05mv2g3izovjyr8qcki.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff05mv2g3izovjyr8qcki.png" alt="last Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This practical guide has explored the art of creating cleaner Laravel controllers by leveraging the capabilities of accessors and mutators. By following best practices and incorporating these potent tools, you can substantially improve the quality of your  codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What You Need to Know About Controllers: &lt;a href="https://www.roberthalf.com/us/en/insights/career-development/all-you-need-to-know-about-controller-salary-levels-jobs" rel="noopener noreferrer"&gt;https://www.roberthalf.com/us/en/insights/career-development/all-you-need-to-know-about-controller-salary-levels-jobs&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Laravel Documentation: &lt;a href="https://laravel.com/docs/10.x/eloquent-mutators" rel="noopener noreferrer"&gt;https://laravel.com/docs/10.x/eloquent-mutators&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>laravel</category>
      <category>api</category>
      <category>php</category>
      <category>programming</category>
    </item>
    <item>
      <title>Step-by-Step Guide on How to Send Error Messages from Laravel App to Slack Using Webhooks</title>
      <dc:creator>Michael Menebraye</dc:creator>
      <pubDate>Mon, 08 Jan 2024 07:37:20 +0000</pubDate>
      <link>https://dev.to/hummingbed/step-by-step-guide-on-how-to-send-error-messages-from-laravel-app-to-slack-using-webhooks-894</link>
      <guid>https://dev.to/hummingbed/step-by-step-guide-on-how-to-send-error-messages-from-laravel-app-to-slack-using-webhooks-894</guid>
      <description>&lt;p&gt;Error messages serve a vital role in web development by identifying and addressing issues within an application. Developers rely on these messages for valuable information that facilitates debugging and troubleshooting. Typically containing details about the nature and location of errors, these messages help developers quickly pinpoint and resolve issues. Their significance spans across development, testing, and production phases, contributing to a smoother and more efficient development cycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importance of HTTP Error Messages in Web Development:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Issue Identification:&lt;/strong&gt; Error messages play a vital role in pinpointing problems within code, including syntax errors, runtime issues, and logical errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Debugging Support:&lt;/strong&gt; The information provided in error messages aids developers in debugging, helping them trace and resolve the root cause of the problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security Considerations:&lt;/strong&gt; Careful management of error messages is crucial to avoid exposing sensitive information, making generic messages preferable in a production environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Logging and Monitoring:&lt;/strong&gt; Error messages are logged for further analysis, enabling monitoring tools to track application health and performance.&lt;/p&gt;

&lt;p&gt;By comprehensively addressing error messages, developers enhance the reliability, security, and overall quality of web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Webhook
&lt;/h2&gt;

&lt;p&gt;Webhooks are automated messages sent from one application to another when specific events occur. They have a message, or payload, and are sent to a unique URL. This allows apps to push data to each other and not waste time checking and waiting. Similarly, Slack employs user-defined HTTP callbacks, known as Webhooks, to notify your Slack application about events from external services like Laravel in real-time. In essence, the webhook URL acts as a channel, transmitting messages from your Laravel app to your Slack channel seamlessly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpaogzrlzs6927cyphlq3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpaogzrlzs6927cyphlq3.png" alt="slack webhook diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the purpose of forwarding error messages from your Laravel applications to your Slack workspace?
&lt;/h2&gt;

&lt;p&gt;Imagine being a member of a team that is building a large Laravel application. Upon completing the project, the Laravel app was deployed to the production environment; deliberately setting the &lt;strong&gt;APP_DEBUG&lt;/strong&gt; configuration in your environment file to &lt;strong&gt;false&lt;/strong&gt;. This deliberate choice means that only a standard HTTP 500 server error message is shown instead of detailed error messages when issues arise. In this scenario, if users face problems while using the Laravel app in the production environment, the development team is unaware of these errors and when a user reports the issue, the development team may find it challenging to pinpoint the exact cause of the problem since they lack information about the specific error. To address this challenge, a Slack webhook can be employed to instantly notify the development team of errors as they occur, providing real-time insights into the exact error message and facilitating prompt resolution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Laravel and PHP Basics&lt;/li&gt;
&lt;li&gt;Composer and PHP installed&lt;/li&gt;
&lt;li&gt;Slack workspace&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that you have a fundamental understanding of webhooks and understand the benefits of integrating Slack webhooks in Laravel applications, the next step is to delve deeper by integrating Slack webhooks into the application. To begin, we need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a new Laravel application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set up a Slack account and configure the Slack webhook.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Integrate the Slack webhook into your Laravel application.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1: Create a new Laravel application.
&lt;/h2&gt;

&lt;p&gt;Go to your terminal or command prompt and paste the following command to create a new &lt;a href="https://laravel.com/docs/10.x/installation#creating-a-laravel-project" rel="noopener noreferrer"&gt;Laravel app&lt;/a&gt;  called slack-webhook.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer create-project laravel/laravel slack-webhook


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2: Set up a Slack account and configure the Slack webhook.
&lt;/h2&gt;

&lt;p&gt;Head to &lt;a href="https://slack.com/intl/en-gb/help/articles/206845317-Create-a-Slack-workspace#desktop-1" rel="noopener noreferrer"&gt;Slack documentation&lt;/a&gt; to create a new workspace if you don't have one. After setting up your slack workspace, click on  the &lt;strong&gt;Add channel&lt;/strong&gt; button on the left-side navigation to create a new channel that will receive error messages. For this tutorial, assign the name &lt;strong&gt;laravel-error-message&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd10ddrtbr4l6ovzf50yg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd10ddrtbr4l6ovzf50yg.png" alt="Slack channel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With your channel successfully created, let's move on to configuring the Slack webhook. Click the dropdown menu next to your Slack workspace name at the top of the left-side navigation on your screen. Subsequently, select &lt;strong&gt;Tools &amp;amp; settings&lt;/strong&gt; and proceed to click on the &lt;strong&gt;Manage app&lt;/strong&gt; button. This will redirect you to a new page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwz0sbn9is3a5yqzvqti1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwz0sbn9is3a5yqzvqti1.gif" alt="Slack webhook config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Proceed to click on &lt;strong&gt;Build&lt;/strong&gt; located on the right side of the top navigation bar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzla3t59wivtxw2gid3ui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzla3t59wivtxw2gid3ui.png" alt="Slack webhook config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following that, click on the &lt;strong&gt;Create An App button&lt;/strong&gt;. This action will prompt a modal pop-up card to appear. On the modal pop-up card, select the &lt;strong&gt;From Scratch&lt;/strong&gt; option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbfckw1fnbbf8wbytqjj3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbfckw1fnbbf8wbytqjj3.png" alt="Slack webhook config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Proceed to enter a name in the &lt;strong&gt;App Name&lt;/strong&gt; input field, and then select your Slack workspace name from the dropdown menu below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjch24afou76mooax3a4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjch24afou76mooax3a4f.png" alt="Slack webhook config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, Activate your Slack webhook by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigating to &lt;strong&gt;Incoming Webhooks&lt;/strong&gt; on the left-side navigation&lt;/li&gt;
&lt;li&gt;Toggle the Activate Incoming Webhook switch to enable it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc2qvrberwf1k4o5do8zo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc2qvrberwf1k4o5do8zo.jpg" alt="Slack webhook config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once your webhook has been activated, scroll down the page and choose &lt;strong&gt;Add a New Webhook to Workspace&lt;/strong&gt;. This will redirect you to a new page where you can select a channel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw40f0tgnsulrmmhprh99.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw40f0tgnsulrmmhprh99.png" alt="Slack webhook config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, choose the &lt;strong&gt;laravel-error-message&lt;/strong&gt; channel created earlier. Click the Allow button to provide permission for sending messages to the Slack channel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feljll4tiaeteardxt0ow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feljll4tiaeteardxt0ow.png" alt="Slack webhook config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have successfully generated our Slack webhook URL. Scroll to the bottom of the webpage to find the Slack webhook we generated. If you have cURL installed on your local machine, you can test the webhook by copying the &lt;strong&gt;Sample curl request to post to a channel&lt;/strong&gt; URL and executing it in your terminal. Afterward, check your Slack channel; you will receive a "hello world" message. Please note that we will not be using the &lt;strong&gt;Sample curl request to post to a channel&lt;/strong&gt; URL in our Laravel app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnso7mu6uc5xzyq4ap2f4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnso7mu6uc5xzyq4ap2f4.png" alt="Slack webhook config"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: Integrate the Slack webhook into your Laravel application.
&lt;/h2&gt;

&lt;p&gt;In this article, we'll utilize Slack incoming webhooks to post messages from our Laravel application by sending JSON payloads to our Slack channel. We'll be implementing the error message scenario mentioned earlier in our application. Here is a process flow diagram illustrating how the application will function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftoss4sd9uyao57vqxcof.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftoss4sd9uyao57vqxcof.png" alt="Slack webhook process flow diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Start by launching the Laravel application created previously using your chosen code editor. Then, proceed to the config directory and open the &lt;strong&gt;logging.php&lt;/strong&gt; file. Inside the logging.php file, find the slack driver, and copy the &lt;strong&gt;LOG_SLACK_WEBHOOK_URL&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

'slack' =&amp;gt; [
  'driver' =&amp;gt; 'slack',
  'url' =&amp;gt; env('LOG_SLACK_WEBHOOK_URL'),
  'username' =&amp;gt; 'Laravel Log',
  'emoji' =&amp;gt; ':boom:',
  'level' =&amp;gt; env('LOG_LEVEL', 'critical'),
  'replace_placeholders' =&amp;gt; true,
],


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above configurations help tailor how Laravel logs messages to the Slack channel, defining details such as where to send the logs, how to format them, and under what conditions they should be logged.&lt;/p&gt;

&lt;p&gt;Now, locate the .env file and follow these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;paste the LOG_SLACK_WEBHOOK_URL&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy the webhook URL generated in the Slack configuration and assign it to 'LOG_SLACK_WEBHOOK_URL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;scroll to the top of the .env file and configure APP_DEBUG=false.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0pob6pmj6j30i18whqnx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0pob6pmj6j30i18whqnx.jpeg" alt="SLACK WEBHOOK URL"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the web.php file in the routes directory and enhance the default route by incorporating an exception.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Route::get('/', function () {
  throw new Exception("Error Processing Request", 1);
  return view('welcome');
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Navigate to the &lt;strong&gt;Handler.php&lt;/strong&gt; file in the &lt;strong&gt;app/Exceptions&lt;/strong&gt; directory and revise the &lt;strong&gt;register&lt;/strong&gt; method with the provided code. The following code retrieves the details of the error messages that will be sent to Slack.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

use Illuminate\Support\Facades\Log;

public function register(): void
  {
      $this-&amp;gt;reportable(function (Throwable $e) {
          Log::channel('slack')-&amp;gt;error($e-&amp;gt;getMessage(),[
              'file' =&amp;gt; $e-&amp;gt;getFile(),
              'Line' =&amp;gt; $e-&amp;gt;getLine(),
              'code' =&amp;gt; $e-&amp;gt;getCode(),
          ]);
      });
  }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Start the application by executing the command below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan serve


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Go to your home page and refresh; you should encounter a 500 server error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsl3di565cj9kmts8mrma.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsl3di565cj9kmts8mrma.png" alt="http 500 error page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, check your Slack channel to view the error message sent from your Laravel application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2fvnrblzyttol2dh7752.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2fvnrblzyttol2dh7752.png" alt="Slack output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations on successfully completing this step-by-step guide. At this point, you have acquired essential knowledge about the significance of implementing this process in your Laravel application. Feel free to leave a comment in the section below, and also share your thoughts on whether I should create similar tutorials or enhance this article.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
