Comments (3)
Hi @Lulu2021-ux
Thank you, we're glad you're enjoying the book :)
Yes you're right, it is because the call to the AllowedFilter method cannot be serialized.
The reason why we are calling this in the config file, is just for convenience, so that we can easily call different types of allowed filters, such as partial
and scope
filters, which Spaties QueryBuilder also supports.
To fix this issue, you could move these calls into the JSONApiService
class and create a method for handling filters.
But before you do this, you'll have to find a way to tell the filter name and filter type in the config file. A way to do this could be like this, where the key is the filter name and the value is the filter type:
'users' => [
//...
'allowedFilters' => [
'role' => 'exact',
],
//...
],
Then in the JSONApiService
we'll add a handleFilters
method, with a parameter to get the type of the resource.
protected function handleFilters(string $type)
{
return collect(config("jsonapi.resources.{$type}.allowedFilters"))
->map(function ($filterType, $filterName) {
switch ($filterType) {
case 'exact':
return AllowedFilter::exact($filterName);
case 'partial':
return AllowedFilter::partial($filterName);
case 'scope':
return AllowedFilter::scope($filterName);
}
})
->toArray();
}
In this method we will get the allowed filters from the config file, for the given type and just to make it easy for us to map the key and value into the correct AllowedFilter
class, we will be using a collection.
When mapping the filter type and name, we can use a simple switch statement to ensure that the types are mapped correctly and that the filter name will be given. The allowedFilters method in the QueryBuilder
takes an array of filters, which means we will have to convert the collection to an array in the end.
In the fetchResources
method you can then simple call the handleFilters
method like this:
public function fetchResources(string $modelClass, string $type)
{
$models = QueryBuilder::for($modelClass)
->allowedSorts(config("jsonapi.resources.{$type}.allowedSorts"))
->allowedIncludes(config("jsonapi.resources.{$type}.allowedIncludes"))
->allowedFilters($this->handleFilters($type))
->jsonPaginate();
return new JSONAPICollection($models);
}
And afterwards the config file should be cacheable again.
Don't forget to run the it_can_filter_administrators_by_role
and it_can_filter_users_by_role
in the Features\UserTest
testclass, so that you ensure that the refactoring doesn't break anything.
Hope this helps :)
from build-an-api-with-laravel.
Thank you a lot for your answer, it really helps 👍 :)
from build-an-api-with-laravel.
You're welcome @Lulu2021-ux 🙂👍
from build-an-api-with-laravel.
Related Issues (8)
- Question regarding Unique Email Users HOT 4
- Unsupported grant type
- Illuminate\Routing\Exceptions\UrlGenerationException Missing required parameters for [Route: books.relationships.authors] [URI: api/v1/books/{book}/relationships/authors]. HOT 3
- Test failing for BooksCollection Resource for the included top-level members HOT 3
- Unique constraint is not working from jsonapi config file HOT 9
- Call to a member function first on null HOT 3
- Polymorphism HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from build-an-api-with-laravel.