Laravel

How to make a trait in Laravel

In today’s article, we will learn about how to make a trait in Laravel and how to use it laravel.

Before moving forward let’s know what trait is.

What is Traits in Laravel?

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).

Traits Syntax:

<?php
trait TraitName {
  // some code...
}
?>

In this article we will know following things.

  • Laravel Command make:trait
  • Laravel Trait path
  • How to make a traits in Laravel
  • How to use trait in controller

Laravel Command make:trait

-> There is no such command available like make:trait as traits are comes under the PHP OOPS.

Laravel Trait path

-> In case of trait path, you can create it anywhere you want, But I suggest you create under App. Under App create a folder name trait and the trait folder create a file name as AuthUserTrait.php, then your final path will like this App/Traits/AuthUserTrait.php

How to make a Trait in Laravel

-> To make a trait in Laravel, open your AuthUserTrait.php file and write the below, I have make this trait to check the user Authentication and to get the user Auth Details.

<?php

namespace App\Traits;
use Auth;

/**
 * this trait is use for identify the current user
 */
trait AuthUserTrait
{
    public function getCurrentUser()
    {
        return Auth::user();
    }
}

How to use Trait in Controller

-> Now I will write the code to show you how we can use the trait in Laravel.

First thing we have to add our trait namespace on the top like the below one

<?php

namespace App\Http\Controllers\Attendance\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Traits\AuthUserTrait; //Auth User Trait

Now we have to use trait in our controller class like the below one

class AttendanceController extends Controller
{   
    use AuthUserTrait;

Now we will call our trait in the below manner

public function getStudent()
    {   
        $user = $this->getCurrentUser(); // getCurrentUser function present in our trait

Our full code will like the below one

<?php

namespace App\Http\Controllers\Attendance\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Traits\AuthUserTrait;

class AttendanceController extends Controller
{   
    use AuthUserTrait;
    
    /**
     * Get student according to teacher ID.
     *
     * @return \Illuminate\Http\Response
     */
    public function getStudent()
    {   
        $user = $this->getCurrentUser();
    }
}

Read Also: One to one relationship in Laravel 8

I hope you like the above blog, and you understood how we can use traits in Laravel.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button