Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

asadsajjad's avatar

Trying to get property 'likes' of non-object (View: /app/resources/views/products/productView.blade.php)

on my local server this is working and on hosting it is giving error. I dont know where I am doing this wrong

Trying to get property 'likes' of non-object (View: /app/resources/views/products/productView.blade.php)

Query $findLikes = Comment::where('user_id', Auth::user()->id)->where('product_id', $id)->first();

View @if((Auth::check()) && ($findLikes->likes == '1'))

<a href="/unlike-product/{{$p->id}}" id="myNumber" name="likes" value="0" onclick="myFunction()"><i class="fa fa-heart-o"></i> UnLike</a> </small>

@elseif((Auth::check()) && ($findLikes->likes == '0'))

<a href="/liked-product/{{$p->id}}" id="myNumber" name="likes" value="1" onclick="myFunction()"><i class="fa fa-heart-o"></i> Like it</a> </small>

@endif

Any help would be great

0 likes
6 replies
tykus's avatar

If/when this expression returns null

 $findLikes = Comment::where('user_id', Auth::user()->id)->where('product_id', $id)->first();

then the following expression will not work:

$findLikes->likes == '1'

You would need to check if there is a found comment (because the query returns a Comment) for the authenticated user and the current product:

@if((Auth::check()) && $findLike)
	@if ($findLikes->likes == '1') 
		// etc
	@elseif($findLikes->likes == '0')
		// etc
	@endif
@endif
asadsajjad's avatar

@tykus I tried that now its not displaying anything. Is it because of the NULL passing ?

tykus's avatar

It is not displaying anything, because there is nothing to display; the result of the Comment query is null.

asadsajjad's avatar
asadsajjad
OP
Best Answer
Level 1

fixed it. The error was in the view as it was not taking the value from following table. Thanks everyone.

Please or to participate in this conversation.