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

DaNeubi's avatar

Updating a Model's value updates anotherone aswell

I'm trying in my opinion something very basic. I have a function that determines wether to create a new entry to the database or update a existing one. The problem I have is, that the "available_begin" value gets updated aswell even though I only update the "available_end" value.

private function appendAsAvailable(Company $company, string $status)
{
    $date = date('Y-m-d H:i:s');
    // check if the company currently is available
    if (Str::contains($status, 'Leitstelle besetzt') && !$company->last_available)
    {
        // JUST OPENED
        $available = new Available();
        $available->company_id = $company->id;
        $available->available_begin = $date;
        $available->save();
        $company->last_available = true;
        $company->save();
    }
    // if not currently available, check if the last status has been available
    else if (!Str::contains($status, 'Leitstelle besetzt') && $company->last_available)
    {
        // JUST CLOSED
        $available = Available::where('company_id', '=', $company->id)->orderBy('id', 'desc')->first();
        if (!$available) return;
        $available->available_end = $date;
        $available->save();
        $company->last_available = false;
        $company->save();
    }
}

If now the $available in the else if statement gets updated, the object itself is correct, but the values that get written to the databse is two times the "available_end" value, thus the "available_start" value gets overwritten. This happens at saving the object. I check that there is no double iteration of my loop or anything like that.

https://cdn.discordapp.com/attachments/915227683716231220/1017482837752041524/unknown.png

this is what it looks like in the database. I debugged the code with xDebug and thus can clearly say that this happens while the object gets updated.

I'm using PHP 8.1 with the latest Laravel 9.x

Thanks ahead for any help

0 likes
1 reply
DaNeubi's avatar

I fixed it by changing the column value from timeStamp to dateTime. Can someone explain why this fixed it?

Please or to participate in this conversation.