Traits are not a Laravel feature. They are a PHP language feature. A Trait is a way to reuse methods across multiple classes. Think of it as a copy-paste helper for classes, but done properly by PHP.
Just like PHP has:
classfunctioninterfacetrait
Think of it like this:
Function = one piece of code
Trait = a collection of reusable code
Class = a blueprint for objectsWhen learning Laravel, you’ll often see code like this:
class User extends Model
{
use HasFactory, Notifiable;
}
Many beginners wonder: What is use? What are Traits? Are they a Laravel feature?
Let’s keep it simple.
What Is a Trait?
A Trait is a PHP feature that allows you to reuse code across multiple classes.
Think of a trait as a container that holds methods and properties that can be shared by different classes.
Why Do We Need Traits?
Imagine you have two classes:
class User
{
public function sayHello()
{
return 'Hello';
}
}
class Admin
{
public function sayHello()
{
return 'Hello';
}
}
The same method is duplicated in both classes.
Instead, we can place the method inside a trait:
trait Greets
{
public function sayHello()
{
return 'Hello';
}
}
And use it in multiple classes:
class User
{
use Greets;
}
class Admin
{
use Greets;
}
Now both classes have access to the sayHello() method without duplicating code.
Is a Trait a Function?
No.
A function is a single block of code:
function sayHello()
{
return 'Hello';
}
A trait is a collection of reusable methods and properties:
trait Logger
{
public function log()
{
// ...
}
public function write()
{
// ...
}
}
So:
- Function = one reusable action
- Trait = a reusable group of methods and properties
Are Traits Part of Laravel?
No.
Traits are a built-in feature of PHP.
Laravel simply uses them heavily to add functionality to classes.
For example:
class User extends Model
{
use HasFactory, Notifiable;
}
Here:
HasFactoryadds factory-related functionality.Notifiableadds notification-related functionality.
Both are traits provided by Laravel, but the trait concept itself comes from PHP.
Traits vs Inheritance
A helpful way to think about it:
class User extends Model
{
use Notifiable;
}
This means:
Useris aModel(inheritance)Userhas notification functionality (trait)
In simple words:
extends= “is a”use= “has this functionality”
Conclusion
Traits are a PHP feature that allows you to share code between multiple classes without duplication.
Laravel uses traits extensively because they provide a clean way to add functionality to classes without creating complex inheritance structures.
If you’re new to Laravel, just remember:
Traits are reusable pieces of code that can be plugged into any class using the use keyword.
In real Laravel projects
You’ll commonly create:
- Controllers
- Models
- Form Requests
- Resources
- Services
Traits are less common and usually created for shared helper behavior such as:
- Slug generation
- Audit logging
- UUID generation
- Common model scopes
- Reusable model events
Simple rule
Ask yourself:
“Do I need exactly the same methods in multiple classes?”
- Yes → A trait might be a good fit.
- No → Don’t create a trait.
Traits are useful, but they’re not something you should create just because Laravel uses them internally. They’re a tool for solving code duplication problems.
Creating a Trait is Very Simple
Usually developers just create the file manually:
app/Traits/HasSlug.php<?php
namespace App\Traits;
trait HasSlug
{
//
}That’s all.
