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

Verdemis's avatar

Update with save() doesn't work

Hello everyone,

I have a strange problem. I'm trying to update a database entry with the save function, but the changed columns won't change. I don't get any errors.

This is the code in my update function:

    $deliveryTime = DeliveryTime::where('key','=',$key)->first();

    $deliveryTime->name = $request->name;
    $deliveryTime->short_name = $request->short_name;
    $deliveryTime->color = $request->color;
    $deliveryTime->save();
    
    return redirect()->action('DeliveryTimeController@edit',$deliveryTime->key)->with(['success' => 'Delivery time updated.']);

When I print the variables p.ex. $request->name I get the correct value. When I print the $deliveryTime->name after I changed it, I get the correct value too.

It looks like the save() doesn't execute. The redirect is executed.

I used this way for updating entries in my database a lot and until now I never had any problems and I don't get where the problem is.

Maybe someone has an idea?

Best, Verdemis

0 likes
30 replies
Talinon's avatar

Are you sure you are actually getting a DeliveryTime record with your where condition? Try firstOrFail() to check?

$deliveryTime = DeliveryTime::where('key','=',$key)->firstOrFail();
Verdemis's avatar

hi Talinon, yes I do. I checked the values in $deliveryTime and also tried firstOrFail()... no errors.

_Artak_'s avatar

use update method

$data = [
    'name' =>  $request->name,
    'short_name' => $request->short_name,
    //..............
];

DeliveryTime::where('key','=',$key)->update($data);


lostdreamer_nl's avatar

is key unique in the table? or could it be that you're updating 1 row, and checking another?

Do you have event listeners for the 'saving' event of the DeliveryTime model? If so, it could be that 1 returns false, and stops the saving.

Verdemis's avatar

And some additional informations. I'm using the debugbar and there is no query listed about an update. I can see the select query for the deliveryTime but no query for the save() function

Verdemis's avatar

Hi lostdreamer_nl, yes key is unique. And no I don't have event listeners for the saving event

Verdemis's avatar

@Artak Ok update does work. But there has to be a reason why save won't work ...

Talinon's avatar

Do you have a primaryKey specified on your model?

protected $primaryKey = 'someColumn';
_Artak_'s avatar

@Verdemis everything seems correct, but try this

$deliveryObj = new DeliveryTime;

$deliveryTime = deliveryObj->where('key','=',$key)->firstOrFail();

    $deliveryTime->name = $request->name;
    $deliveryTime->short_name = $request->short_name;
    $deliveryTime->color = $request->color;
    $deliveryTime->save();


Verdemis's avatar

@Talinon no I don't have specified a primary key in the model. the primary key is 'id'

Verdemis's avatar

When I use the update method like this

$data = [ 'name' => $request->name, 'short_name' => $request->short_name, 'color' => $request->color ];

DeliveryTime::where('key','=',$key)->update($data);

it does work.

But when I try to use the record I get in the first row of my code it doesn't work.

$deliveryTime = DeliveryTime::where('key','=',$key)->firstOrFail();

$data = [ 'name' => $request->name, 'short_name' => $request->short_name, 'color' => $request->color ];

$deliveryTime->update($data);

But I don't get any error messages either. And when I print p.ex. $deliveryTime->name I get the correct value...

newbie360's avatar

can you try see any errors ?

....
$deliveryTime->color = $request->color;

DB::beginTransaction();
try
{
    $deliveryTime->save();
    DB::commit();
} catch (\Throwable $e) {
    DB::rollBack();
    dd($e);
    return redirect()->back()->withErrors(['error' => $e->getMessage()]);
}
ryanborum's avatar

You might need to specify your fillable fields in your model:

class MyModelName extends Model
{

    protected $fillable = ['id, 'name', 'etc];

}
Verdemis's avatar

@ryanborum I tried...

protected $fillable = ['key','name','short_name','color'];

makes no difference

Verdemis's avatar

@newbie360 of course

namespace App;

use Illuminate\Database\Eloquent\Model;

class DeliveryTime extends Model { protected $table = 'delivery_time'; protected $fillable = ['key','name','short_name','color'];

public function items()
{
    return $this->hasMany('App\Item','key','delivery_time');
}

}

staudenmeir's avatar

What's the result of dd($deliveryTime->isDirty()); before you call save()?

_Artak_'s avatar

what is output of

        \DB::listen(function ($query) {
            dump($query->sql);
            // $query->bindings
            // $query->time
        });

// ... you code
$deliveryTime->save();

dd('ok');


_Artak_'s avatar

except "ok" is there sql query too???

Verdemis's avatar

@Artak I tried to get the last query...

"update delivery_time set name = ?, updated_at = ? where id is null"

And this looks "slightly" wrong... but why do I get such a weired query...

_Artak_'s avatar

show your migration file, and what is the output


 $deliveryTime = DeliveryTime::where('key','=',$key)->first();

dd( $deliveryTime->id);

newbie360's avatar

check the key value, because the key = key not found

you may use ->firstOrFail() // direct to 404

or

$deliveryTime = DeliveryTime::where('key', $key)->first();

if (blank($deliveryTime))
{
    return back()->with('wrong_key', 'the key not found');
}
Verdemis's avatar
Verdemis
OP
Best Answer
Level 1

I reworked the whole script and now it works, but I still don't have a clou what exactly was the problem. But now it works and I can go on. Thanks to everyone who tried to help :)

pinakmithaiwala's avatar

$deliveryTime = DeliveryTime::select('Primary_Key')->where('key','=',$key)->first();

$data['name '] = $request->name; $data['short_name '] = $request->short_name; $data['color '] = $request->color;

DeliveryTime::find($deliveryTime->Primary_Key)->update($data);

Please or to participate in this conversation.