Laravel

Query builder

PHP autodoc-laravel recognizes your query builder methods and uses them to accurately document your API responses and TypeScript types.

Example

return response()->json(
    Planet::select('id', 'diameter')
        ->with('spaceStations:id,name')
        ->where('has_rings', true)
        ->get()
);

By default, autodoc-laravel skips unknown or dynamic methods in a query chain. To stop analyzing any chain that contains one, set laravel.abandon_query_builder_parsing_on_unknown_methods to true.

Supported methods

Result shapes are inferred for these chain-ending methods:

all, avg, average, count, create, doesntExist, exists, find, findOrFail, first, firstOrCreate, firstOrFail, firstOrNew, firstWhere, get, paginate, pluck, sum, updateOrCreate

How result types are inferred:

  • select, addSelect and column lists passed to get([...]) narrow the resulting shape to the selected columns, including aliases.
  • Eager-loaded relations from with(...) are resolved into nested shapes - see Eloquent models for the relation requirements.
  • find and findOrFail return a collection when the key argument is array-like, a single model otherwise.
  • Scalar methods use fixed return types: count returns a non-negative integer; exists and doesntExist, a boolean; sum, a number; and avg and average, a number or null. Static model calls such as Planet::sum('diameter') use the same types.

Pagination

paginate() resolves to the full LengthAwarePaginator JSON shape - data items typed from the query, plus current_page, last_page, per_page, total, from, to, links and the page URL fields. The page query parameter is added to the documented route automatically, and a custom page name passed to paginate() is honored:

return response()->json(
    Rocket::paginate(100)
);

Raw queries

Chains rooted in the DB facade (DB::table(...), DB::query()) can contain subqueries, unions and raw expressions, so no row shape is inferred for them. They still resolve usefully:

  • get and pluck return a collection, so chained collection methods with callbacks keep working.
  • The scalar finishers listed above return their fixed types.

Database transactions

DB::transaction(...) resolves to the return type of its callback, which is analyzed like any other closure:

$order = DB::transaction(function () {
    // ...

    return Order::create([...]);
});