1

I am making a request from my database in function but then I get an error trying to get property of non-object. But nothing is wrong with my code for it to return the error

>>trying to get property of non-object.  

Controller

 public function shopHook(Request $request)
    {
        $shop = $request->getContent();
        $shop = json_decode($shop, true);
        $shop_id = $shop['number'];
        $get_msg = ShopCancelled::where('name',Auth::user()->domain)->first();
    }

Web Route

Route::post('shop-cancel-webhook', 'AppController@shopHook');

Why could this be happening? The code seems to be breaking at

$get_msg = ShopCancelled::where('name',Auth::user()->domain)->first();  

When making the request

6
  • 2
    Seems like Auth::user() is null. Is the authentication working properly? Commented Nov 5, 2018 at 14:37
  • 1
    check if the error happens when you access domain: Auth::user()->domain Commented Nov 5, 2018 at 14:38
  • try replacing Auth::user()->domain with some dummy string to see if error goes away. Commented Nov 5, 2018 at 14:41
  • If you are requesting it in API, auth session is not properly set. If I'm right, first of all you need to look up about api auth Commented Nov 5, 2018 at 14:42
  • @Phiter, okay so Auth::user() is null and when i hardcoded the domain in replace, it worked. But why is it null ? Commented Nov 5, 2018 at 14:43

2 Answers 2

1

I think the problem is in this line:

$shop_id = $shop['number'];

try instead $shop_id = $shop->number;

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

1 Comment

If you have red the comments above, its all about auth. Auth::user() is returning null =))
0

That error is because you are trying to access to a property inside an object that has not been instantiated or doesn't have that property. Would be helpful if you can point out exactly the line where the error is presented.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.