FelixL's avatar

Observer fails in test, works in browser

I have a laravel livewire test that executes the save method on an eloquent model. The model has an observer which creates 11 child models.

$parent->child()->create(['year' => $i])

This child model has an observer that sets 3 properties when it is created depending on values of the partent model. It accesses them with:

$parent = $child->parent;
if ($parent->property) {

This works when I test it in my browser but fails with 'Attempt to read property "property" on null'. The child model has the relationship key set when I view it in debug mode.

$child->parent_id: 2

The model methods find() and all() also don't work. Did I miss a setting or does someone have an idea how to debug this?

0 likes
1 reply
FelixL's avatar
FelixL
OP
Best Answer
Level 2

My problem was that I have a global scope in the parent model class that I didn't fill correctly with a session variable. The variable wasn't set to the right value.

Please or to participate in this conversation.