1. Go to this page and download the library: Download bjerke/api-query-builder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bjerke / api-query-builder example snippets
public function index(Request $request)
{
// Setup the builder
$queryBuilder = new QueryBuilder(new MyModel, $request);
// Parse the request and return an Eloquent Builder instance
$query = $queryBuilder->build();
// The instance can be extended/modified freely just as any other Eloquent Builder instance
// For example, maybe we want to enable the option to turn pagination on/off?
if (($pagination = $request->input('paginate')) !== null &&
($pagination === false || $pagination === 'false' || $pagination === '0')
) {
return $query->get();
}
$perPage = $request->input('per_page');
return $query->paginate($perPage)->appends($request->except('page'));
}
public function index(Request $request)
{
// Setup the builder
$queryBuilder = new QueryBuilder(new MyModel, $request);
// Parse the request and return an Eloquent Builder instance
$query = $queryBuilder->build();
// The instance can be extended/modified freely just as any other Eloquent Builder instance
// For example, maybe we want to enable the option to turn pagination on/off?
if (($pagination = $request->input('paginate')) !== null &&
($pagination === false || $pagination === 'false' || $pagination === '0')
) {
return $query->get();
}
$perPage = $request->input('per_page');
return $query->paginate($perPage)->appends($request->except('page'));
}
// ...
public function allowedApiFields(): array
{
// Default is ['*']
return [
'firstname',
'lastname'
];
}
// ...
// ...
public function allowedApiRelations(): array
{
return [
'user' // Must be a relation on your model
];
}
// ...
// ...
public function allowedApiCounts(): array
{
return [
'user' // Must be a relation on your model
];
}
// ...
// ...
public function allowedApiAppends(): array
{
return [
'full_name' // Must be an appendable attribute on your model
];
}
// ...
sh
php artisan vendor:publish --provider="Bjerke\ApiQueryBuilder\QueryBuilderServiceProvider"