1

I am trying to grab a model and update one of the fields but i keep receiving this error message.

Creating default object from empty value

 if(isset($data['code']) && $data['code']){
            $invite = \App\Invite::where('code', $data['code'])->first();
            $invite->accepted = 1;
            $invite->save();
        }

I have never seen this error before am i grabbing the model incorrectly when updating the accepted field?

2
  • dd($invite); to see if you actually get something back Commented Nov 10, 2020 at 12:24
  • $invite is null, so you getting this error. As lagbox said already as answer. I suggest you to use firstOrFail() instead of first(), then it will throw a 404 error instead of this error. Commented Nov 10, 2020 at 12:27

1 Answer 1

2

first() can return null. So your query did not return a result so $invite is null. If you try to assign a property to null you get that error, as it creates a default object and sets that property on it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.