Site Admin
Site Admin Founder of MeaningArticles
1188 Views

Laravel 8 Traits Example Tutorial

Hello Dev.

Now let's have a look at instance 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. allow's discuss about how to use traits in laravel 8.

In this article we're going to study Laravel 8 traits, how to create Trait in laravel 8, and how to use Trait within the laravel 8 software or application. traits allow us to develop a reusable piece of code and inject it in controller and modal in a Laravel application.

right here i will provide you with easy and easy manner to use traits instance in laravel eight. In this example i'm going to show total user with table the usage of traits in laravel 8.

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

Step 1: Install Laravel 8

first of all we want to get clean and new Laravel eight version application the use of bellow command, now open your terminal OR command prompt and then fire bellow command and run it:

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

 

Step 2: Configuration of Database

second step, we configure database like database name, username, password etc for application of laravel 8. let's open .env file and fill complete details equal as bellow:
.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name(blog)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)

 

Step 3: Create Traits

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

<?php

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 we can create useful resource route for show users the usage of traits in laravel 8. so permit's add easy routes as like bellow:
routes/web.php

use App\Http\Controllers\UserController;

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

 

Step 5: Create Controller

here, we will create new controller as UserController. so allow's upload bellow code on that controller file.
app\Http\Controllers\UserController

<?php

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.
resources/views/users.blade.php

<!DOCTYPE html>
<html>

<head>
    <title>Laravel 8 Traits Example - meaningarticles.com</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="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" crossorigin="anonymous" />
</head>

<body>
    <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 - meaningarticles.com</h3>
                    </div>
                    <div class="card-body">
                        <table class="table table-bordered">
                            <tr>
                                <th>No.</th>
                                <th>Name</th>
                                <th>Email</th>
                            </tr>
                            @forelse($users as $key => $user)
                                <tr>
                                    <td>{{ ++$key }}</td>
                                    <td>{{ $user->name }}</td>
                                    <td>{{ $user->email }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="3">No Users Found</td>
                                </tr>
                            @endforelse
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

 

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
User::factory()->count(100)->create()


Step 8: Run 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:

http://localhost:8000/users

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.