overtrue / laravel-like

👍 User-like features for Laravel Application.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Method Illuminate\Database\Eloquent\Collection::likes does not exist. When trying to count the number of likes

JamieCee20 opened this issue · comments

On my blade page I have {{ $post->likes()->count() }} to display the number of current likes however when I refresh the page I get the error: Method Illuminate\Database\Eloquent\Collection::likes does not exist.

To my understanding Im not using that so unsure why it wont find the function im looking for.

Any help would be appreciated.

Jamie

/**
 * Display the specified resource.
 *
 * @param  \App\Verified  $verified
 * @return \Illuminate\Http\Response
 */
public function show(Verified $verified)
{
    $user = User::find($verified->user_id);
    return view('verifieds.show', compact('user','verified'));
} 

This is in my post Controller

No I have it as $verified->likers() - >count()

Plz, explain where is the $post defined? paste the controller code and blade template code.

So Due to the way my project pans out, $verified is my post. This is my controller

public function show(Verified $verified)
{
    $user = User::find($verified->user_id);
    return view('verifieds.show', compact('user','verified'));
}

And my blade was like this:

a class="likeButton" href=" $user->toggleLike($verified) "> i class="fas fa-thumbs-up mr-2">

$verified->likers()->count(

Hi, @JamieCee20, I read your code, this error is impossible because $verified will always be an instance of Verified . So please double check your logic to see if there is a variable overwrite.

Verified is still posts and doesnt overrun anywhere. I have Post which is user posts but verified is the same functionality just for verified Users, (May be a less complicated scenario but im still very new to laravel).

So surely if I set my controller to $post = Verified::find({id}) then it will still be the same outcome?

/**
 * Display the specified resource.
 *
 * @param  \App\Verified  $verified
 * @return \Illuminate\Http\Response
 */
public function show(Verified $verified)
{
    $user = User::find($verified->user_id);
    $likes = $user->likes()->with('likeable')->paginate(20);
    return view('verifieds.show', compact('user','verified', 'likes'));
}

This is my controller, verifieds is just a different post so imagine all $verified is $post.

My show.blade.php has this

                                @foreach($likes as $Like)
                                    {{$like->likeable}}
                                @endforeach

to display my information.

and the error I get is Class name must be a valid object or a string