Get in touch

As we know, we must have a thin controller…

Laravel give to us pretty nice posobility for requst validation – request validation Classes. I know… it takes a lot of time…. But when you do that – you will have structured data and always have clear understanding what validation rule is working and where validation class situated…
Please use: artisan make:request requestName comand for creating validation requests.
And don’t forget change returned value of authorize() method

 

Bad practice:

public function store(Request $request)
{
    $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
        'publish_at' => 'nullable|date',
    ]);

    ....
}

 

Good practice

 

public function store(PostRequest $request)
{    
    ....
}

class PostRequest extends Request
{
    public function rules()
    {
        return [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
            'publish_at' => 'nullable|date',
        ];
    }
}

You always need to lvl up yourself