overtrue / laravel-like

πŸ‘ User-like features for Laravel Application.
485 stars 39 forks source link

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

Closed JamieCee20 closed 2 years ago

JamieCee20 commented 4 years ago

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

overtrue commented 4 years ago

Plz show me the controller code.

JamieCee20 notifications@github.com于2020εΉ΄7月30ζ—₯ ε‘¨ε››δΈ‹εˆ11:04ε†™ι“οΌš

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

β€” You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/overtrue/laravel-like/issues/28, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALHOYEBQTS6OQ3S6RQM2YDR6GDZVANCNFSM4PN6SM2Q .

JamieCee20 commented 4 years ago
/**
 * 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

overtrue commented 4 years ago

So, $post is undefined?

JamieCee20 notifications@github.com于2020εΉ΄7月31ζ—₯ ε‘¨δΊ”δΈŠεˆ1:06ε†™ι“οΌš

/**

  • 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

β€” You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/overtrue/laravel-like/issues/28#issuecomment-666529457, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALHOYG536SLSZ74K7E3ZC3R6GSAHANCNFSM4PN6SM2Q .

JamieCee20 commented 4 years ago

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

overtrue commented 4 years ago

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

JamieCee20 commented 4 years ago

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'));
}
JamieCee20 commented 4 years ago

And my blade was like this:

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

$verified->likers()->count(

overtrue commented 4 years ago

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.

JamieCee20 commented 4 years ago

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).

JamieCee20 commented 4 years ago

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

JamieCee20 commented 4 years ago
/**
 * 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