Laravel 8 Traits Example Tutorial

Laravel , Laravel 8

Nicesnippets

470

14-10-2020


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_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

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:

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 of all we will create resource route for show users using traits in laravel 8 . so let's add simple 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 let's add 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 - NiceSnippets.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 - NiceSnippets.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 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:

http://localhost:8000/users

It will help you.....


Recommended Posts