Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

webfuelcode's avatar

How to update the category name

I am trying to update the category name and I found something like this...

public function update(Request $request, Category $category)
    {
        $this->validate($request,[
            'name' => 'required|min:3|max:50'
        ]);

        $category->update($request->all());
        return redirect()->route('cats.index', $category->id)->withMessage('Edited successfuly!');
    }

Which is, I find wrong in my case. I found this on a tutorial and tried to make it for my project updating the category name... I do not understand $category->update($request->all());

  • Can you please explain what $category->update($request->all()); means?
  • And what shout I use in place of this to update the category name?

Currently, I do not see the changes. Please make it work for me.

0 likes
2 replies
Snapey's avatar
public function update(Request $request, Category $category)
    {
        $this->validate($request,[
            'name' => 'required|min:3|max:50'
        ]);

        $category->name = $request->name;
	$category->save();

        return redirect()->route('cats.index', $category->id)->withMessage('Edited successfuly!');
    }

make sure you pass the correct category id into the update route

$request->all() means all the fields passed in the request. It requires a most basic understanding of Laravel and probably indicates you need to watch some of Jeffrey's videos.

1 like
ExpDev07's avatar
ExpDev07
Best Answer
Level 1

I would do it like this:

public function update(Request $request, Category $category)
    {
        $validated = $this->validate($request,[
            'name' => 'required|min:3|max:50'
        ]);

        $category->update($validated);
        return redirect()
            ->route('cats.index', $category->id)
            ->withMessage('Edited successfuly!');
    }
1 like

Please or to participate in this conversation.