🚀 Laravel Tip💡: Extract Validated Input Elegantly

El Morjani Mohamed - Jul 21 - - Dev Community

As developers, we often need to extract only a few items from a validated request. Instead of manually unsetting or filtering, Laravel provides a clean and efficient way to do this using the safe() method. Here’s how:

// Extract only specific fields
$validated = $request->safe()->only(['name', 'email']);

// Extract all fields except specified ones
$validated = $request->safe()->except(['name', 'email']);

// Extract all validated input
$validated = $request->safe()->all();
Enter fullscreen mode Exit fullscreen mode

Using safe() makes your code cleaner and ensures you're only working with validated data, enhancing both security and code clarity.

. . . .
Terabox Video Player