Laravel Traits in Action Use Cases and

Laravel Traits in Action: Use Cases and Implementation

Laravel Traits in Action: Use Cases and Implementation

Hello guys, in this tutorial we will learn About Laravel Traits in Action: Use Cases and Implementation.


What are Traits?

In PHP, Traits is a simple way to reuse code. Trait is a collection of methods. To stop the repetition, follow the DRY (Don’t Repeat Yourself) approach. As we know, PHP is a single inheritance language, that means, one child class only inherits from only one single parent. In that case, Traits can be useful. Traits allow us to remove limitations of single inheritance language by reuse some methods in independently in several class.


Laravel Traits: Use cases

  1. Reuse Code
  2. Reduce limitations a of single inheritance language
  3. Manageable code in a large project
  4. Get a lot done by prebuilt Traits


Laravel Traits in Action: Implementation

Lets create ‘Traits’ folder in app/http, then create a trait named ‘EmployeeTrait’ . Put the below code in that file (app/Http/Traits/EmployeeTrait.php).

namespace App\Http\Traits;
use App\Models\Employee;
trait EmployeeTrait {
  public function index() {
    // Fetch all the employees from the 'employee' table.
    $employees = Employee::all();
    return view('employees')-&amp;gt;with(compact('employees '));

This above code is a trait, which will fetch all the employees from our Employee table, and it will allow us to use in any controller.


Now we will use this Trait in a Controller. Here we have an Salary Controller, where we will use EmployeeTrait. For that, first go to the routes file to add a route:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EmployeeController;
| Web Routes
Route::get('/', function () {
    return view('welcome');
Route::resource('employees', EmployeeController::class);


After that we will create a controller named “EmployeeController” to us that EmployeeTrait. For that run:

php artisan make:controller EmployeeController

Now insert the following code in your EmployeeController

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Traits\EmployeeTrait;
class EmployeeController extends Controller
    use EmployeeTrait;

If you closely check, there is no index method for fetching these employees from the table. Our EmployeeTrait is doing that for us. We are just using that trait in this controller.

As your Trait implementation is done, now we will show these employee data in our view file. Go to below blade file and the following code.


<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<meta charset="utf-8">
<title>Laravel Traits Example</title>
<link rel="stylesheet" href="">
<div class="container mt-5">
<table class="table table-inverse">
<th>Employee ID</th>
@foreach($employees as $employee)
<tr id="employee{{$employee->id}}">


Our work is done. Run the application by running php artisan serve command.

Open the url in browser and see magic:


Here is our output:



If you have any questions, you can comment below. If you loved this article, show love by sharing the article who might need it.

Have a nice day. Keep learning! See you in the next one.


Editorial Staff

A Learner and trying to share what I learned!