PHPackages                             charlesuwaje/response-macros - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. charlesuwaje/response-macros

ActiveLibrary[API Development](/categories/api)

charlesuwaje/response-macros
============================

Standard API response macros for Laravel.

15Blade

Since Jul 23Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/Charlesuwaje/response-macros)[ Packagist](https://packagist.org/packages/charlesuwaje/response-macros)[ RSS](/packages/charlesuwaje-response-macros/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

📦 Response Macros for Laravel
=============================

[](#-response-macros-for-laravel)

A lightweight Laravel package that provides reusable JSON response macros such as `success`, `error`, and more — to make your API responses consistent and developer-friendly. This package provides a set of reusable HTTP response macros for Laravel, helping you maintain consistent and readable API responses across your entire application.

---

🚀 Installation
--------------

[](#-installation)

### ✅ For Production (Packagist Release)

[](#-for-production-packagist-release)

> This requires the package to be published on [Packagist](https://packagist.org/)

```
composer require charlesuwaje/response-macros
---
##  For Development (Local Git Repository)
> composer require charlesuwaje/response-macros:@dev
```

---

📦 Development Installation (local or GitHub repo)
-------------------------------------------------

[](#-development-installation-local-or-github-repo)

{ "minimum-stability": "dev", "prefer-stable": true, "repositories": \[ { "type": "vcs", "url": "[https://github.com/YOUR\_USERNAME/response-macros](https://github.com/YOUR_USERNAME/response-macros)" } \] }

```
composer require charlesuwaje/response-macros:@dev
```

---

📦 Registering the Macros
------------------------

[](#-registering-the-macros)

In your App\\Providers\\AppServiceProvider.php (or a custom provider), add this in the boot() method:

```
use CharlesUwaje\ResponseMacros\Helpers\ResponseMacro;

public function boot(): void
{
    ResponseMacro::register();
}
✅ After this step, Laravel's response() helper will now support the new macros globally.
```

---

Available Response Macros
-------------------------

[](#available-response-macros)

MacroStatus CodeDescription`response()->success($message, $data)`200Standard success response`response()->created($message, $data)`201Successful resource creation`response()->error($message, $statusCode)`CustomGeneric error with optional code`response()->unauthorized($message)`401User not authenticated`response()->forbidden($message)`403User lacks permission`response()->notFound($message)`404Resource not found`response()->validationError($message, $errors)`422Validation failed`response()->noContent()`204Empty success response---

🧪 Usage in Your Controller
--------------------------

[](#-usage-in-your-controller)

Once the macro is registered, you can use it like a native Laravel response inside any controller:

```
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        $users = User::all();

        return response()->success('Users retrieved successfully', $users);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
        ]);

        $user = User::create($validated);

        return response()->created('User created successfully', $user);
    }

    public function show($id)
    {
        $user = User::find($id);

        if (! $user) {
            return response()->notFound('User not found');
        }

        return response()->success('User found', $user);
    }

    public function destroy($id)
    {
        $user = User::find($id);

        if (! $user) {
            return response()->notFound();
        }

        $user->delete();

        return response()->noContent();
    }
}
```

---

JSON Output Format
------------------

[](#json-output-format)

The macros will return JSON responses with the following structure:

```
{
    "status": "success",
    "message": "Your message here",
    "data": {
        // Data to be returned
    }
}
```

For errors:

```
{
    "status": "error",
    "message": "Your error message here",
    "errors": {
        // Validation errors
    }
}
```

---

💡 Best Practices
----------------

[](#-best-practices)

Use response()-&gt;success() for all 200-level responses

Use response()-&gt;error() with custom codes if needed

Use response()-&gt;validationError() to structure validation failure messages

Consistent responses help frontends, mobile apps, and API consumers parse data easily
-------------------------------------------------------------------------------------

[](#consistent-responses-help-frontends-mobile-apps-and-api-consumers-parse-data-easily)

📚 Contributing
--------------

[](#-contributing)

PRs and suggestions are welcome! If you find a bug or want to add new macros, feel free to open an issue or submit a pull request.

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance40

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/045f9ed4c79bf3330e40f2104e1cc70db421f89e282603d4c28b648a59d5efe9?d=identicon)[Charlesuwaje](/maintainers/Charlesuwaje)

---

Top Contributors

[![Charlesuwaje](https://avatars.githubusercontent.com/u/105389447?v=4)](https://github.com/Charlesuwaje "Charlesuwaje (17 commits)")

### Embed Badge

![Health badge](/badges/charlesuwaje-response-macros/health.svg)

```
[![Health](https://phpackages.com/badges/charlesuwaje-response-macros/health.svg)](https://phpackages.com/packages/charlesuwaje-response-macros)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M475](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M270](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M453](/packages/google-gax)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
