When working with Laravel, we often come across terms like Stubs, Accessors, Mutators, and Relations. These concepts make development faster, more maintainable, and cleaner. Let’s break them down with simple explanations and practical examples.
📌 What are Laravel Stubs?
Laravel stubs are template files used by Artisan commands (like make:model
, make:controller
, etc.) to generate code files with pre-defined content.
Why Use Stubs?
By publishing and customizing stubs, you can:
- Predefine common methods you want in every controller or model.
- Follow your project’s coding standards every time you generate files.
- Save time by avoiding repetitive boilerplate code.
Example: Publishing Stubs
To publish stubs, run:
php artisan stub:publish
This command will generate stub files in: /stubs
For example, the controller.stub
might look like this:
<?php
namespace {{ namespace }};
use App\Http\Controllers\Controller;
class {{ class }} extends Controller
{
//
}
Customizing a Stub
If you want every new controller to have a default index()
method, you can modify controller.stub
like this:
public function index()
{
return view('welcome');
}
Now, every time you create a controller with:
php artisan make:controller SampleController
It will already have the index()
method — saving you time.
📌 What are Laravel Accessors?
Accessors are methods in your Eloquent model that modify a field’s value when retrieving it from the database.
Example Use Case
Let’s say your database has a first_name
column, but you want to always display the first letter capitalized whenever you access it.
Defining an Accessor
In your User
model:
class User extends Model
{
// Accessor for first_name
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
}
Now, if first_name
is stored as john
, accessing it via $user->first_name
will automatically return John
.
Key Naming Rule
Accessors must follow this naming convention:
get{ColumnName}Attribute
For the first_name
column, it becomes: getFirstNameAttribute
📌 What are Laravel Mutators?
Mutators are the opposite of Accessors. They allow you to modify a value before saving it to the database.
Example Use Case
If you want to always store first names in lowercase, you can create a mutator.
Defining a Mutator
In your User
model:
class User extends Model
{
// Mutator for first_name
public function setFirstNameAttribute($value)
{
$this->attributes['first_name'] = strtolower($value);
}
}
Now, if you do:
$user->first_name = 'John';
It will be saved as john
in the database.
Key Naming Rule
Mutators must follow this naming convention:
set{ColumnName}Attribute
For the first_name
column, it becomes: setFirstNameAttribute
📌 What are Laravel Relationships?
Relationships in Laravel allow you to define associations between tables/models without writing raw SQL join queries.
Types of Relationships
Laravel supports the following:
- One to One
Example: Each user has one profile. - One to Many
Example: A user has many posts. - Many to One
Example: Each post belongs to one user (reverse of One to Many). - Many to Many
Example: Users can belong to many roles, and roles can be assigned to many users.
Example 1 – One to One Relationship
In User
model:
public function profile()
{
return $this->hasOne(Profile::class);
}
Now, you can access:
$user->profile; // Fetch user's profile
Example 2 – One to Many Relationship
In User
model:
public function posts()
{
return $this->hasMany(Post::class);
}
Now, you can access:
$user->posts; // Fetch all posts belonging to the user
Example 3 – Many to Many Relationship
In User
model:
public function roles()
{
return $this->belongsToMany(Role::class);
}
This assumes you have a pivot table like role_user
.
You can now do:
$user->roles; // Fetch all roles assigned to the user
Why Use Relationships?
- Clean, expressive code (no manual joins).
- Eager loading support (
with()
method) for performance. - Easy-to-maintain and change.
📌 Quick Comparison
Feature | Purpose | Location |
---|---|---|
Stub | Customize artisan-generated files (controller, model, etc.) | /stubs folder |
Accessor | Modify field value when retrieving from DB | Model |
Mutator | Modify field value before saving to DB | Model |
Relation | Define relationships between tables (One to One, Many to Many, etc.) | Model |
Conclusion
Laravel’s Stubs, Accessors, Mutators, and Relations are all tools that help developers write clean, efficient, and maintainable code. Whether you want to customize generated files, format data automatically, or define clean table relationships, Laravel has you covered.