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

Kieran_st's avatar

save() returns true yet does not update

Hi all. I'll start by saying yes, I have looked this us, and no, none of the fixes for other people seem to work for me...

So I have a player model which I want to update a particular field on. I submit an ajax request, and have it update the relevant field.

This model uses a customised primary key.

Player Model

class Player extends Model
{
    // Settings
    protected $primaryKey = 'playerid';
    protected $connection = 'mysql_game';
    protected $table = "players";

    public $timestamps = false;

    // Various casting and accessor/mutator stuff
    // Various relationships
}

The table has the following: https://puu.sh/zXh3Y/dd75b2e0d9.png Plus a 'access' field which is type TEXT

Now... In my controller (yea I got the logic in the controller atm, I'll be extracting it later)... It's a patch route which accepts an action as one of the request inputs. It switches through those inputs and then updates the relevant section(s) based on the action provided.

Anyway... the issue here is only for one of the actions. None of the others have this issue, even though the saving code itself is identical. The player is grabbed at the start of the controller before any of the actions are switched through, so the player object is identical for both the working and not working switches.

Controller (part of only)

public function update(Request $request)
    {
        $output = [
            'status' => 'failure',
            'message' => 'Nothing happened',
            'data' => null
        ];

        if (!$request->has(['id','action','data'])){
            $output['message'] = "Missing input parameters!";
            return $output;
        }

        $data= $request->get('data');
        $id= $request->get('id');

        $player = Player::find($player);
        if (is_null($player)){
            $output['message'] = "Could not find player";
            return $output;
        }

        switch($request->get('action')){
            // various switch actions in here
        }

The layout of the $player->access array is: [ ['keyname',0], ['keyname2',0], ['keyname3',1], ... ] Basically an array of arrays, with each sub-array containing the value name and a 1/0 (true/false) value. The Player model typecasts this into an array when accessing.

case "faction.access": {
                $access= $player->access;
                $key = null;
                foreach ($access as $i=>$item){
                    if ($item[0] === $data){
                        $key = $i;
                    }
                }

                $newValue = 0;
                if ($access[$key][1] === 0){
                    $newValue = 1;
                }

                $log = new LogCreator();
                $log->setTarget($player->playerid)->setAction('access-'.$value)->setOld($access[$key][1])->setNew($newValue)->save();

                $access[$key][1] = $newValue;
                $player->access = $access;

                try {
                    $player->save();
                    $this->breakPlayerCache($player->playerid);
                    $output['status'] = 'success';
                    $output['data'] = $access;
                } catch (\Exception $e){
                    $output['message'] = 'An error occurred when saving.';
                }
                break;
            }

Now, I get the 'success' message on my frontend, and the data which is passed back is correct... however, when I check the database, nothing was saved. When I used Barryvdh's Debugbar to output the values, I can also see that the $access variable has been updated correctly, but the $player object doesn't actually get those values saved to it.

I've tried this out in artisan tinker as well with the same result. And save() returns true.

Most the other things I've found on the web have been about either not specifying the custom primary key (which I have done), or by having an uppercase/lowercase issue (all my stuff is lowercase)... So I'm completely stumped... Any help would be greatly appreciated.

(Crossposted on StackOverflow : https://stackoverflow.com/questions/49685840/laravel-eloquent-model-save-doesnt-update-database )

0 likes
5 replies
Jacobs's avatar

As suggested on stackoverflow, your player model is missing the array of fillable fields:

protected $fillable = [
    'access',
    ....
];
petrit's avatar

Or you can do the opposite of fillable

protected $guarded = ['id'];
...
Kieran_st's avatar

Tried both of those with no success.

Updating any other field for the player table works. It's just that one which doesn't

rin4ik's avatar

$player = Player::find($player); can you explain this line? what value has $player inside find();

Kieran_st's avatar
Kieran_st
OP
Best Answer
Level 9

Turns out that I had made a stuff up in an accessor. which explains why only that field had an issue saving.

Feel rather stupid now xD

Thanks to everyone who threw suggestions my way :)

Please or to participate in this conversation.