Laravel 9 Traits Example Tutorial

May 06, 2022 . Admin

Hi Guys,

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

In this article we are going to learn about Laravel 9 Traits, how to create Trait in laravel 9, and how to use Trait in the laravel 9 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 9. In this example i am going to show total user with table using traits in laravel 9.

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

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Setup Database

After successfully install laravel 9 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 9. 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: Create Route

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


use App\Http\Controllers\UserController;

| 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!

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 9 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 9 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
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:


It will help you....

#Laravel 9