Laravel Macros: Extend Framework Features
January 16, 2026
•
1 min read
•
22 views
Table of Contents
Extend Laravel with macros:
Response Macro
Response::macro('success', function ($data = null) {
return Response::json([
'success' => true,
'data' => $data,
]);
});
Collection Macro
Collection::macro('toUpper', function () {
return $this->map(fn($value) => strtoupper($value));
});
Builder Macro
Builder::macro('whereLike', function ($column, $value) {
return $this->where($column, 'LIKE', "%{$value}%");
});
Usage
return response()->success($data);
$items->toUpper();
User::whereLike('name', 'john')->get();
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!