How to assign a value to a field in a model?

,

Hello, dear Kirby community!
Can you help me assign a field to a different variable in a model? What’s the correct way to do this?

class OfferPage extends Kirby\Cms\Page

{

    public function offer()

    {

        $this->abonement()->old_price = 99; // <--- how to assign value?

        $offer = $this->abonement()->toObject();  

        return $offer;

    }

}

I tried the following code:

class OfferPage extends Kirby\Cms\Page

{

    public function offer()

    {

        $offer = $this->abonement()->toObject();

        $offer->old_price = 99; // <--- try to assign value


        dump($offer);

        die();


        return $offer;

    }

}

As a result, the object still contains an empty old_price field:

Kirby\Cms\Content Object
(
    [price] => 1800
    [old_price] => 
    [promocode_enabled] => true
    [promocode] => SUPER
    [discount] => 100
    [comment_enabled] => false
    [comment_placeholder] => 
    [no_address] => false
)

You cannot set props like that dynamically.

$offer = $this->abonement()->toObject();
$offer = $offer->update(['old_price' => 99]); // this updates the content object in memory only
dump($offer);

Thanks for the tip, texnixe! That’s just what I needed!