DEV Community

Netsai
Netsai

Posted on

1

Laravel API Response Helpers

A simple package allowing for consistent API responses throughout your Laravel application.
Install the package:
composer require f9webltd/laravel-api-response-helpers

<?php

namespace App\Http\Api\Controllers;

use F9Web\ApiResponseHelpers;
use Illuminate\Http\JsonResponse;

class OrdersController
{
    use ApiResponseHelpers;

    public function index(): JsonResponse
    {
        return $this->respondWithSuccess();
    }
}
Enter fullscreen mode Exit fullscreen mode

Other Methods that can be used

respondNotFound(string|Exception $message, ?string $key = 'error')
Enter fullscreen mode Exit fullscreen mode

Returns a 404 HTTP status code, an exception object can optionally be passed.

`respondWithSuccess(array|Arrayable|JsonSerializable|null $contents = null)`
Enter fullscreen mode Exit fullscreen mode

Returns a 200 HTTP status code, optionally $contents to return as json can be passed. By default returns ['success' => true].

respondOk(string $message)
Enter fullscreen mode Exit fullscreen mode

Returns a 200 HTTP status code

respondUnAuthenticated(?string $message = null)
Enter fullscreen mode Exit fullscreen mode

Returns a 401 HTTP status code


respondForbidden(?string $message = null)
Enter fullscreen mode Exit fullscreen mode

Returns a 403 HTTP status code

respondError(?string $message = null)
Enter fullscreen mode Exit fullscreen mode

Returns a 400 HTTP status code

respondCreated(array|Arrayable|JsonSerializable|null $data = null)
Enter fullscreen mode Exit fullscreen mode

Returns a 201 HTTP status code, with response optional data

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more