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
composer require martinpham/laravel-openapi-generator
php artisan vendor:publish --tag=openapi-generator
php artisan openapi:generate
SwaggerUI will be available at /api/openapi
<?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