1.3 KiB
1.3 KiB
Adapters
Adapters connect your API schema to your application's data persistence layer.
You'll need to supply an adapter for each resource type you define. You can define resource types using the resource method. For example:
use Tobyz\JsonApiServer\Schema\Type;
$api->resourceType('users', $adapter, function (Type $type) {
// define your schema
});
Eloquent Adapter
An EloquentAdapter is provided out of the box to hook your resources up with Laravel Eloquent models. Instantiate it with the model class that corresponds to your resource.
use App\Models\User;
use Tobyz\JsonApiServer\Adapter\EloquentAdapter;
$adapter = new EloquentAdapter(User::class);
When using the Eloquent Adapter, the $model passed around in the schema will be an instance of the given model, and the $query will be a Illuminate\Database\Eloquent\Builder instance querying the model's table:
$type->scope(function (Builder $query) {});
$type->attribute('name')
->get(function (User $user) {});
Custom Adapters
For other ORMs or data persistence layers, you can implement your own adapter.