Requests

Allow requests through your application to send specific parameters like filters, includes, appends, fields, sorts and search.

As a new addition to this package, you can now allow your users to use:

Remember that there are 2 ways to achieve the exact same behaviour on this package, you can use PHP Attributes or normal methods. Advantage of these is that attributes can be at the method or class level.

All of the following being conditionally allowed by you on your controllers, like the following example:

/**
 * Display a listing of the resource.
 *
 * @return \OpenSoutheners\LaravelApiable\Http\Resources\JsonApiCollection<\App\Models\Post>
 */
public function index()
{
    return JsonApiResponse::from(Post::class)
        ->allowing([
            AllowedFilter::similar('title'),
            AllowedFilter::similar('film.title'),
            AllowedFilter::similar('content'),
            AllowedSort::make('created_at'),
            AllowedSort::make('user_id'),
            AllowedInclude::make('author'),
        ]);
}

Allow includes

You can allow users to include relationships to the JSON:API response.

You may also allow nested includes like so:

Or using a more specific method like allowIncludes:

Allow sorts

You can allow your users to sort by descendant or ascendant order (or both, which is the default behaviour).

As top uses the direction defined in the config as default you can specify a direction instead, the following are all doing the same but in different directions:

Default sorts can be applied by using the following method:

Allow filters

Remember that a similar filter is using LIKE comparison at the end (on the database), while an exact is using MATCH (or =). Use them depending of your case. By default it uses LIKE comparison.

You can allow your users to filter by a model attribute or its relation's attributes.

By default will use similar (LIKE) but you may change this in the config/apiable.php file:

You can also specify any method you like:

Scoped filters might be used if you want to filter using Eloquent's query scopes:

And even restrict what they can use for filter on each filter like so:

Same with sorts you can apply default filters whenever a user didn't send any via HTTP query parameters that were allowed:

Allow fields (sparse fieldset)

This part just fully complaints with JSON:API, while the allowAppends doesn't as it's something specially adapted to Laravel.

Allow fields will limit the columns selected by the database query being ran by Laravel.

You can also use models in replace of that first argument (the resource type):

Or directly an array as first argument to append to the main response resource (in this case film):

Allow appends

Same as allowing fields by resource type but this will append Model accessors after the query is done.

You can also use models in replace of that first argument (the resource type):

Or directly an array as first argument to append to the main response resource (in this case film):

This feature is only available for proper setup of Laravel Scout in your model.

You can also perform full-text search with this package with the help of Laravel Scout. So the frontend should send something like yourapi.com/?q=search_query or yourapi.com/?search=search_query) to perform a search if allowed by the backend.

Include allowed filters & sorts on the response

If you have a table or a component in the frontend that needs to know about what can be filtered or sorted by, you may want to add this to your JSON:API response:

Then the response payload will look like this:

Last updated