martinpham/laravel-openapi-generator

Generate OpenAPI specification from Laravel routes and Laravel Eloquent/Laravel Data objects.

v1.0.0 2025-08-04 15:22 UTC

This package is not auto-updated.

Last update: 2025-08-08 15:33:07 UTC


README

This is a fork of https://github.com/xolvionl/laravel-data-openapi-generator with newer Laravel, Spartie Data, and Eloquent model supports

Generate OpenAPI specification from Laravel routes and Laravel Eloquent models & Spartie Data classes

Install

composer require martinpham/laravel-openapi-generator

Config

php artisan vendor:publish --tag=openapi-generator

Generate

php artisan openapi:generate

SwaggerUI

SwaggerUI will be available at /api/openapi

Example

<?php  
namespace App\Data;  
  
use App\Models\User;  
use Spatie\LaravelData\Data;  
  
class GreetingData extends Data  
{  
    public function __construct(  
        public User         $user,  
        public string       $message  
    ){}  
}  
<?php
namespace App\Http\Controllers\Api;  

use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Group;
use App\Data\GreetingData;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Collection;
use App\Models\User;

#[Group('/play', as: 'play')]
class PlaygroundApiController extends Controller
{  
    #[Get('/who-is/{user}', name: ".whois")]
    public function hello(User $user): User
    {
        return $user;
    }

    
    #[Get('/user', name: ".user")]
    /** @return Collection<int, User> */
    public function hello(): Collection
    {
        return User::all();
    }

    #[Get('/hello/{user}', name: ".hello")]
    public function hello(User $user): GreetingData
    {
        return GreetingData::from($user, "hello ${user->name}");
    }
}

Results https://imgur.com/a/Mt3qa51