octal
6 months ago

Update vs Save model after form validation

Posted 6 months ago by octal

Hello, I'm having a strange behaviour (or I didn't understood something in laravel doc).

I have a simple form that has a PATCH action that trig my controller update method. The code is the following (simplified)

 public function update(Request $request, Product $product)
    {
        $attributes = $request->validate(
            [
                'label'       => ['required', 'min:3'],
                'description' => [],
                'category'    => []
            ]);

        $product->update($attributes);
        return redirect("/products/{$prodcut->id}");
    }

I have put all my three attributes in the $fillable field of my Product model.

When I use this code, everything works as exected (the model is updated). If I replace the $product->update with $product->save($attributes) the rediection occurs correctly and everything seems to work as expected, but when I check my database, the model is NOT UPDATED at all.

What's the real difference between save and update ????? what am I missing here?

Please sign in or create an account to participate in this conversation.