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
Laravel Rest API supports [Laravel’s Precognition](https://laravel.com/docs/precognition) feature for live frontend validation.
5
+
Precognition allows you to anticipate the outcome of an HTTP request by running validation rules without executing the controller logic.
6
+
In other words, the request is “simulated” purely for validation.
7
+
8
+
## Installation
9
+
10
+
Precognition support is disabled by default. To enable it, set `rest.precognition.enabled` to true in your config/rest.php.
11
+
:
12
+
```php
13
+
// config/rest.php
14
+
'precognition' => [
15
+
'enabled' => true,
16
+
],
17
+
```
18
+
19
+
This flag toggles Laravel’s `HandlePrecognitiveRequests` middleware support.
20
+
21
+
## Usage Example
22
+
23
+
Once enabled, any API endpoint generated via `Rest::resource` will automatically have `HandlePrecognitiveRequests` applied.
24
+
This means that any request with the `Precognition` header to true will be considered precognitive.
25
+
For example, consider a search endpoint on the users resource. If the client sends:
26
+
27
+
```http request
28
+
POST /api/users/search
29
+
Precognition: true
30
+
Content-Type: application/json
31
+
32
+
{
33
+
"search": {
34
+
// ... search parameters ...
35
+
}
36
+
}
37
+
```
38
+
39
+
Laravel will execute all middleware and run the form request or validator rules as usual, then immediately return the validation result. In this mode:
40
+
- If the data fails validation, you will receive a 422 Unprocessable Entity with the validation errors.
41
+
- If it passes validation, you will receive a 200 OK response and no database action is taken.
42
+
43
+
In short, the request is validated but the controller method is never called, allowing you to get “live” validation feedback from the API without performing the actual operation.
0 commit comments