Request Safe to get data from request safely in Laravel

Sometimes we need specific attributes only from requests or exclude some attributes from requests…

▶ Here’s the solution:

➡️ To get all data validated, we call:

$request->validated()

👉 To get only specific data (validated), you can use:

$request->safe()->only(['name', 'email']);
Enter fullscreen mode
Exit fullscreen mode

👉 To get data excluding specific data (validated), you can use:

$request->safe()->except(['name', 'email']);
Enter fullscreen mode
Exit fullscreen mode

Keep in your mind there is major different between ( $request->only() & $request->except() ) and ($request()->safe()->only() & $request->safe()->except() )

Request safe return inputs data after validation, on the other hand, request->except() will retrieve any data sent from the input if it isn’t inside the validation.

So make sure you take all data validated to prevent your model from infected mass assignment.

That’s all, Happy coding 🥳


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top