You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 14, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+75-6Lines changed: 75 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,15 @@
6
6
7
7
Integrate JSON:API resources on Laravel.
8
8
9
+
## Features
10
+
11
+
- Compatible and tested with all the Laravel LTS supported versions ([see them here](https://laravel.com/docs/master/releases#support-policy))
12
+
- Full formatting using pure built-in model methods and properties.
13
+
- Relationships and nested working with [eager loading](https://laravel.com/docs/master/eloquent-relationships#eager-loading).
14
+
- Permissions "out-of-the-box" authorising each resource view or list of resources.
15
+
- Auto-hide not allowed attributes from responses like `user_id` or `post_id`.
16
+
- Own testing utilities built-in Laravel's ones to make integration tests easier.
17
+
9
18
## Installation
10
19
11
20
You can install the package via composer:
@@ -14,7 +23,7 @@ You can install the package via composer:
14
23
composer require skore-labs/laravel-json-api
15
24
```
16
25
17
-
###Usage
26
+
## Usage
18
27
19
28
As simple as importing the class `SkoreLabs\JsonApi\Http\Resources\JsonApiCollection` for collections or `SkoreLabs\JsonApi\Http\Resources\JsonApiResource` for resources.
20
29
@@ -43,6 +52,34 @@ class UserController extends Controller
43
52
}
44
53
```
45
54
55
+
### Custom resource type
56
+
57
+
To customise the resource type of a model you should do like this:
58
+
59
+
```php
60
+
<?php
61
+
62
+
namespace SkoreLabs\JsonApi\Tests\Fixtures;
63
+
64
+
use Illuminate\Database\Eloquent\Model;
65
+
use SkoreLabs\JsonApi\Contracts\JsonApiable;
66
+
67
+
class Post extends Model implements JsonApiable
68
+
{
69
+
/**
70
+
* Get a custom resource type for JSON:API formatting.
71
+
*
72
+
* @return string
73
+
*/
74
+
public function resourceType(): string
75
+
{
76
+
return 'custom-post';
77
+
}
78
+
}
79
+
```
80
+
81
+
**Note: Just remember to check the allowed types in [the oficial JSON:API spec](https://jsonapi.org/format/#document-member-names).**
82
+
46
83
### Authorisation
47
84
48
85
For authorize a resource or collection you'll need the `view` and `viewAny` on the **model policy**, which you can create passing the model to the make command:
@@ -58,12 +95,44 @@ Alternatively, you can pass an authorisation (boolean) to the constructor of the
58
95
return new JsonApiResource($user, true);
59
96
```
60
97
61
-
## Features
98
+
## Testing
62
99
63
-
- Full formatting using pure built-in model methods and properties.
64
-
- Relationships and nested working with [eager loading](https://laravel.com/docs/master/eloquent-relationships#eager-loading).
65
-
- Permissions "out-of-the-box" authorising each resource view or list of resources.
66
-
- Auto-hide not allowed attributes from responses like `user_id` or `post_id`.
In case of a test that **receives a JSON:API collection** as a response, **the first item will be the defaulted** on all the methods that tests **attributes and relationships**.
114
+
115
+
In case you want to access a specific item you have the `at` method:
0 commit comments