Laravel

Eloquent resources

PHP autodoc-laravel supports Laravel API resources, enabling automatic response documentation from JsonResource and ResourceCollection classes.

Example

return new UserResource($user);
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin User
 */
class UserResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

Note that @mixin tag is necessary to correctly read model properties in the resource class. This tag will also help if you are using a tool like PHPStan to analyze your code.