Skip to content

Commit f53afcd

Browse files
committed
Update 3.gates.md
1 parent c190b3f commit f53afcd

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

content/4.digging-deeper/3.gates.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,45 @@ class UserResource extends Resource
3838
// ...
3939
}
4040
```
41+
42+
## Policy message in gates
43+
44+
You are able to get policies message if you want to be more explicit about why the policy is not passing, you first need to set the
45+
config `rest.gates.message.enabled` to true. Be aware that this will change the gates format
46+
47+
In your policy you need to return a policy message:
48+
49+
```php
50+
use App\Models\Post;
51+
use App\Models\User;
52+
use Illuminate\Auth\Access\Response;
53+
54+
/**
55+
* Determine if the given post can be updated by the user.
56+
*/
57+
public function update(User $user, Post $post): Response
58+
{
59+
return $user->id === $post->user_id
60+
? Response::allow()
61+
: Response::deny('You do not own this post.');
62+
}
63+
```
64+
65+
and this will result in the change of the `search` gating format:
66+
67+
```json
68+
{
69+
"data": [
70+
{
71+
"id": 1,
72+
"name": "Lou West",
73+
"gates": {
74+
"authorized_to_update": {
75+
"allowed": false,
76+
"message": "You do not own this post."
77+
}
78+
}
79+
}
80+
]
81+
}
82+
```

0 commit comments

Comments
 (0)