Laravel - Validation - Working With Validated Input

After validating incoming request data using a form request or a manually created validator instance, you may wish to retrieve the incoming request data that actually underwent validation. This can be accomplished in several ways. First, you may call the validated method on a form request or validator instance. This method returns an array of the data that was validated:

    
    $validated = $request->validated();
    
    $validated = $validator->validated();
	

Alternatively, you may call the safe method on a form request or validator instance. This method returns an instance of Illuminate\Support\ValidatedInput. This object exposes only, except, and all methods to retrieve a subset of the validated data or the entire array of validated data:

    
    $validated = $request->safe()->only(['name', 'email']);
    
    $validated = $request->safe()->except(['name', 'email']);
    
    $validated = $request->safe()->all();
	

In addition, the Illuminate\Support\ValidatedInput instance may be iterated over and accessed like an array:

    
    // Validated data may be iterated...
    foreach ($request->safe() as $key => $value) {
        //
    }
    
    // Validated data may be accessed as an array...
    $validated = $request->safe();
    
    $email = $validated['email'];
	

If you would like to add additional fields to the validated data, you may call the merge method:

    
    $validated = $request->safe()->merge(['name' => 'Taylor Otwell']);
	

If you would like to retrieve the validated data as a collection instance, you may call the collect method:

    
    $collection = $request->safe()->collect();