Daxsis
3 months ago

cannot reload after save hasOne relationship both ways

Posted 3 months ago by Daxsis

Is it possible to have a relationship hasOne() both ways on different models and reload them after the model Order has been saved?

For example: Purchaser:

    public function order()
    {
        return $this->hasOne(Order::class);
    }

Order

    public function purchaser()
    {
        return $this->hasOne(Purchaser::class);
    }

The idea is to have ids on both tables for reference. I have primary keys on both tables.

However if I $order->save() the relationships are gone. When I try $order->fresh([...relationships..]); I got null on every relation.

Even if I try to fetch it again it returns null

Order::where('id', $order->id)->with([
            'purchaser',
        ])->get();

I'm processing a payment in DB transaction to create save and then fetch again before the commit has been done. it might affect stuff because I tried all the options I know in Tinker and it returned loaded relations.

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