10-Apr-2023
.
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: Database Configuration
After successfully install laravel 9 Application, Go to your project .env file and set up database credential and move next step :
.env
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: Add 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:
app/Traits/UserTrait.php
<?php
namespace App\Traits;
use App\Models\User;
trait UserTrait {
/**
* Write code on Method
*
* @return response()
*/
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 9 . so let's add simple routes as like bellow:
routes/web.php
<?php
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: Add Controller
Here, we will create new controller as UserController. so let's add bellow code on that controller file.
app\Http\Controllers\UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Traits\UserTrait;
class UserController extends Controller
{
use UserTrait;
}
Step 6: Add Blade 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 9 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 9 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: Add 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()
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:
http://localhost:8000/users
It will help you.....
#Laravel 9