Request Safe to get data from request safely in Laravel

RMAG news

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’]);

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

$request->safe()->except([‘name’, ’email’]);

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 🥳

Please follow and like us:
Pin Share