Support the ongoing development of Laravel.io →
posted 6 years ago
Requests

By the way i am just editing the default boilerplate, so are these the same?

 public function update(Request $request, Project $project)
    {
        $project->title = $request->input('title');
        $project->save();
        return redirect('/projects');
    }

and

public function update(Project $project)
    {
        $project->title = request('title');               
        $project->save();
        return redirect('/projects');
    }

and if yes whats the benefit of using the first way over the second since its even longer.

and is it possible to use this:

$project->update(request(['title', 'description'])); 

with $request->..... and mass assign it like the first method?

I could not make a new reply hence this edit.

i was not talking about form-request (i didn't know about this so i thought you were right but that is an entirely different thing).

from what i have learnt(i am still learning) passing Request $request we can link the input to it which is considered cleaner since we don't have to nest it.(Haven't heard of any better reason yet).

Last updated 3 years ago.
0

With the first method, Laravel can automatically inject an appropriate type of request, which can have different properties. Mostly this is useful for route and method specific validation.

If you have

public function update(ProjectUpdateRequest $request, Project $project)
{
    $project->title = $request->input('title');
    $project->save();
    return redirect('/projects');
}

You can than have a ProjectUpdateRequest class with rules just for a Project when it's being updated.

class ProjectUpdateRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => [
                'required',
                'max:255',
            ],
        ];
    }
}
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Asim iteni iteniasim Joined 2 Nov 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.