DEV Community

Yasser Elgammal
Yasser Elgammal

Posted on

4 1

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 🥳

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay