Laravel 8 Traits Example Tutorial

Laravel , Laravel 8




Now let's see example of how to create traits in laravel 8 application. In this article i will show you traits exmaple in laravel 8. We will learn how to create traits in laravel 8. let's discuss about how to use traits in laravel 8.

In this article we are going to learn about Laravel 8 Traits, how to create Trait in laravel 8, and how to use Trait in the laravel 8 application. Traits allow us to develop a reusable piece of code and inject it in controller and modal in a Laravel application.

Here i will give you simple and easy way to use traits example in laravel 8. In this example i am going to show total user with table using traits in laravel 8.

Now let's see bellow example for how to use traits in laravel 8. So let's follow bellow step by step:

Step 1: Install Laravel Project

First, you need to download the laravel fresh setup. Use this command then download laravel project setup :

composer create-project --prefer-dist laravel/laravel blog

Step 2: Setup Database

After successfully install laravel 8 Application, Go to your project .env file and set up database credential and move next step :




DB_DATABASE=here your database name

DB_USERNAME=here database username

DB_PASSWORD=here database password

Step 3 : Create Traits

Now you can create app folder in Traits/UserTrait.php folder and file, and assimilate all the following code inside of it.

first of all we will create resource route for show users using traits in laravel 8 . so let's add simple routes as like bellow:



namespace App\Traits;

use App\Models\User;

trait UserTrait {

public function index() {

// Fetch all the users from the 'users' table.

$users = User::all();

return view('users')->with(compact('users'));



Step 4 : Add Route

first of all we will create resource route for show users using traits in laravel 8 . so let's add simple routes as like bellow:


use App\Http\Controllers\UserController;

Route::resource('users', UserController::class);

Step 5 : Create Controller

Here, we will create new controller as UserController. so let's add bellow code on that controller file.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Traits\UserTrait;

class UserController extends Controller


use UserTrait;


Step 6 : Create View file

here, we need to create blade file and in this blade file we use users and use their code.


<!DOCTYPE html>



<title>Laravel 8 Traits Example -</title>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="" integrity="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" crossorigin="anonymous" />



<div class="container">

<div class="row mt-5">

<div class="col-10 offset-1 mt-5">

<div class="card">

<div class="card-header bg-info">

<h3 class="text-white">Laravel 8 Traits Example -</h3>


<div class="card-body">

<table class="table table-bordered">






@forelse($users as $key => $user)


<td>{{ ++$key }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>




<td colspan="3">No Users Found</td>











Step 7 : Create Dummy Records:

Here, we need to add some dummy records on users table using factory So let's open terminal run bellow artisan tinker command:

php artisan tinker


Step 8 : Run Your Project

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:


It will help you.....

Recommended Posts