ErrorException: Trying to get property 'id' of non-object --Laravel unit test

698 views Asked by At

Recently I am working on a project 'facebook-clone' using Laravel and vue.js. I have been using unit test for the first time. After performing the test I found this error,

Error: "ErrorException: Trying to get property 'id' of non-object" Error: "ErrorException: Trying to get property 'id' of non-object"

For sending a friend request I wrote this

test.a_user_can_send_a_friend_request a_user_can_send_a_friend_request

I am using many to many relationships in User Model.

many to many relationships within users model

many to many relationships within users model

this is the pivot table friends friends

Api route:

api.php

api.php

The Controller used:

FriendRequestController

FriendRequestController

The resource used:

Friend Friend

1

There are 1 answers

0
Md. Imran Hossain On

The problem is solved, I made a mistake in FriendRequestController where I should have passed an argument in Friendesource, instead I passed an array by mistake.

With error:

return new FriendResource([
        Friend::where('user_id', auth()->user()
        ->id)->where('friend_id', $data['friend_id'])
        ->first()
    ]);

after fixing error:

return new FriendResource(
        Friend::where('user_id', auth()->user()
        ->id)->where('friend_id', $data['friend_id'])
        ->first()
    );