API

All the deep details of this library goes here.

Illuminate\Database\Eloquent\Builder

Please, note the difference between Collection and Builder coming from an Eloquent model, because that conditions the accesibility of these and other methods.

Extending the framework Illuminate\Database\Eloquent\Builder.

Source: OpenSoutheners\LaravelApiable\Builder

jsonApiPaginate

Transforms collection of query results of valid JsonApiable resources to a paginated JSON:API collection (JsonApiCollection).

Parameters:

Name
Default

pageSize

null

columns

['*']

page

null

Example:

App\Models\Post::where('title', 'my filter')->jsonApiPaginate();

Illuminate\Support\Collection

Extending the framework Illuminate\Support\Collection.

Source: OpenSoutheners\LaravelApiable\Collection

toJsonApi

Transforms collection of valid JsonApiable resources to a JSON:API collection (JsonApiCollection).

Note: This method doesn't paginate, for pagination take a look to the Builder::jsonApiPaginate.

Parameters:

None...

Example:

App\Models\Post::where('title', 'my filter')->get()->toJsonApi();

// or

collect([Post::first(), Post::latest()->first()])->toJsonApi();

OpenSoutheners\LaravelApiable\Contracts\JsonApiable

Model contract.

toJsonApi

If the model below implements OpenSoutheners\LaravelApiable\Contracts\JsonApiable and uses the trait OpenSoutheners\LaravelApiable\Concerns\HasJsonApi, you could do the following to transform the model to JSON:API valid response:

$post = App\Models\Post::first();

$post->toJsonApi();

OpenSoutheners\LaravelApiable\Support\Apiable

These methods are available as global helpers functions (see examples).

config

Method used to get user config parameters for this specific package.

Example:

Apiable::config('filters.default_operator', 'default value here');
apiable()->config('filters.default_operator', 'default value here');

toJsonApi

Transform passed value (can be instance of different types: Builder, Model, Collection, etc...).

Example:

$post = Post::first();

Apiable::toJsonApi($post);

// or

$posts = Post::get();

Apiable::toJsonApi($posts);
$post = Post::first();

apiable()->toJsonApi($post);

// or

$posts = Post::get();

apiable()->toJsonApi($post);

resourceTypeForModel

Guess resource type from model class or instance.

Example:

$post = Post::first();

Apiable::resourceTypeForModel($post);

// or

Apiable::resourceTypeForModel(Post::class);
$post = Post::first();

apiable()->resourceTypeForModel($post);

// or

apiable()->resourceTypeForModel(Post::class);

getResourceType

Get resource type from model class or instance (if one specified, otherwise guess it using resourceTypeForModel method).

Example:

$post = Post::first();

Apiable::getResourceType($post);

// or

Apiable::getResourceType(Post::class);
$post = Post::first();

apiable()->getResourceType($post);

// or

apiable()->getResourceType(Post::class);

jsonApiRenderable

Render errors in a JSON:API way. Check documentation on how to integrate this in your project.

Example:

try {
  // Code that might fails here...
} catch (\Throwable $e) {
  Apiable::jsonApiRenderable($e, request());
}
try {
  // Code that might fails here...
} catch (\Throwable $e) {
  apiable()->jsonApiRenderable($e, request());
}

response

Render content as a JSON:API serialised response. Check documentation on how to customise these reponses.

Example:

Apiable::response(Film::all())->allowing([
  // list of allowed user request params...
])->list();

// or

Apiable::response(Film::all(), [
  // list of allowed user request params...
]);
apiable()->response(Film::all())->allowing([
  // list of allowed user request params...
])->list();

// or

apiable()->response(Film::all(), [
  // list of allowed user request params...
]);

Last updated