DEV Community

Yasser Elgammal
Yasser Elgammal

Posted on

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)