Closed eduardoarandah closed 6 years ago
getData() does more than what validated()
method do. in the getData we handle attachment during the create process an during the update process. Also, we handle the boolean value. Please have a look at the getData()
method in the demo https://www.crestapps.com/laravel-code-generator/demos/v2-2
/**
* Get the request's data from the request.
*
*
* @return array
*/
public function getData()
{
$data = $this->only(['name','age','biography','sport','gender','colors','is_retired','range','month']);
if ($this->has('custom_delete_photo')) {
$data['photo'] = null;
}
if ($this->hasFile('photo')) {
$data['photo'] = $this->moveFile($this->file('photo'));
}
$data['is_retired'] = $this->has('is_retired');
return $data;
}
Oh, didn't know that, great!
I was replacing this following line for $this->validated() so I type less code, but I guess they do the same thing:
$data = $this->only(['name','age','biography','sport','gender','colors','is_retired','range','month']);
No worries. Glad your thinking about ways to make the generated code better.
Hey!
There's a function called geData() in the request classes, that ensures only certain fields are passed to save method
I think you should take a look at line 173 in the base form request: https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Http/FormRequest.php
That function returns ONLY fields that exist in $rules array, which is the same thing.
My suggestion would be to use that function insted of getData, removing a little complexity from output code