CrestApps / laravel-code-generator

An efficient Laravel code generator, saving time by automating the creation of resources such as views, controllers, routes, migrations, languages, and form-requests. Highly flexible and customizable, it includes a cross-browser compatible template and client-side validation for application modernization.
https://laravel-code-generator.crestapps.com
MIT License
738 stars 158 forks source link

Enhacement: use request->validated() instead of getData() #69

Closed eduardoarandah closed 6 years ago

eduardoarandah commented 6 years ago

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

MikeAlhayek commented 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;
    }
eduardoarandah commented 6 years ago

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

MikeAlhayek commented 6 years ago

No worries. Glad your thinking about ways to make the generated code better.