Md. Imran Hossain
Md. Imran Hossain

Reputation: 49

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

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

Upvotes: 0

Views: 743

Answers (1)

Md. Imran Hossain
Md. Imran Hossain

Reputation: 49

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()
    );

Upvotes: 0

Related Questions