How to use Traits in Laravel 8

How to use Traits in Laravel 8?

In today’s article, we are going to discuss what is traits, how you can create traits, how to use Traits in laravel application, and how traits are useful in Laravel application.

What is Traits

As we know OOPs (Object-Oriented Programming System) concept and in that we have seen abstract classes and interfaces. A “Trait” is similar to an abstract class, in that it cannot be instantiated on its own but contains methods that can be used in a concrete class.

Traits are a mechanism for code reusability in single inheritance languages such as PHP.

How you can create a Trait

Create a folder under app\Http\Traits and then create your Trait under Trait Folder.

You then need to make sure you namespace your trait with that path, so place:

namespace App\Http\Traits

How Traits will be helpful in Laravel application

Traits are used to reusable pieces of code and code reusability. Traits are a handy way of reusing code in PHP, another way to help keep your code DRY (or “don’t repeat yourself”), which means traits are nothing but a reusable collection of methods and functions that we can use inside our controller.

So let’s understand Traits in Laravel 8 with a live Example:-

We have a products table with some records in our database, so we create traits and get all the products inside our ProductController using Trait.

Define Route

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;


/*
|--------------------------------------------------------------------------
| 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::get('/list-product', [ProductController::class, 'listproduct']);

Create a Trait name QueryTrait.

app\Http\Traits\QueryTrait.php

<?php 

namespace App\Http\Traits;
use App\Models\Product;

trait QueryTrait{

	public function getProductData(){
		$products = Product::get();
		return $products;
	}
}

app\Http\Controllers\ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use App\Http\Traits\QueryTrait;

class ProductController extends Controller
{
   use QueryTrait;

    public function listproduct()
    {

        $productdetil = $this->getProductData();
        dd($productdetil);
    }
}

We call the Method name (getProductData) in our Product Controller.

Run Application :

127.0.0.1:8000/list-product

Now you can see we successfully got the product array using Traits.

In this article, we successfully integrated “How to use Traits in Laravel 8”, I hope this article will help you with your Laravel application Project.

Read also: Laravel Foreign Key

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com