Laravel Traits in Action Use Cases and Implementation_laravelplug.com

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).

[php]

<?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 ‘));
}
}

[/php]

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:

[php]

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EmployeeController;
/*
|————————————————————————–
| Web Routes
|————————————————————————–
*/
Route::get(‘/’, function () {
return view(‘welcome’);
});
Route::resource(’employees’, EmployeeController::class);

[/php]

 

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

[php]
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Traits\EmployeeTrait;
class EmployeeController extends Controller
{
use EmployeeTrait;
}
[/php]

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.

resources/views/welcome.blade.php

[html]

<!DOCTYPE html>
<html lang="{{ str_replace(‘_’, ‘-‘, app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<title>Laravel Traits Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<table class="table table-inverse">
<thead>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($employees as $employee)
<tr id="employee{{$employee->id}}">
<td>{{$employee->id}}</td>
<td>{{$employee->name}}</td>
<td>{{$employee->email}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</body>
</html>

[/html]

 

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

Open the url in browser and see magic:

http://127.0.0.1:8000/employees

 

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.

 

Check Out More Resources:

Articles:
Website: https://laravelplug.com/
YouTube Channel: https://www.youtube.com/channel/UCnTxncHQcBSNs4RCuhWgF-A?sub_confirmation=1
WordPress Playlist: https://www.youtube.com/watch?v=8VvXzFAFwQU&list=PLVoVZqAh8dTLHZ51egvEU7dsOrRCSIMWx
Tools Playlist: https://www.youtube.com/watch?v=gTQoUn3BQkM&list=PLVoVZqAh8dTK-DWHBGWOEEvzvPQrgFne-

WordPress Tutorials: https://laravelplug.com/category/wordpress/
Laravel Tutorials: https://laravelplug.com/category/laravel/
PHP Tutorials: https://laravelplug.com/category/php/
SQL Tutorials: https://laravelplug.com/category/sql/
Various Tips: https://laravelplug.com/category/tips/
Useful Tools: https://laravelplug.com/category/tools/

Socials:

Twitter: https://twitter.com/LaravelPlug
Pinterest: https://www.pinterest.com/LaravelPlugCom/
Facebook: https://www.facebook.com/groups/1020759861842360
Mail: info@laravelplug.com

#laravel #guide #php

 

Editorial Staff

A Learner and trying to share what I learned!