rappasoft / rappasoft-comments

utteranc.es repository for rappasoft.com
2 stars 0 forks source link

blog/building-a-like-button-component-in-laravel-livewire #5

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Rappasoft | Blog | Building a like button component with Laravel Livewire

Follow along as I create a reusable like button in Laravel Livewire that restricts clicks per IP address.

https://rappasoft.com/blog/building-a-like-button-component-in-laravel-livewire

pishguy commented 3 years ago

That was very useful. Thanks so much

rik43 commented 3 years ago

good, but user agent can be longer than 255 characters. better use md5($useragent)

fecony commented 3 years ago

Hey, nice article! I noticed you have a typo in Post model boot method. You have called post a $user 😄 So it looks like you assign user_id to $user and not current post model

 // We will automatically add the user to the post when it's saved.
        static::creating(function ($post) { // <--
            if (auth()->user()) {
                // $user->user_id = auth()->id();
                $post->user_id = auth()->id(); // should be this
            }
        });
rappasoft commented 3 years ago

Hey, nice article! I noticed you have a typo in Post model boot method. You have called post a $user 😄 So it looks like you assign user_id to $user and not current post model

 // We will automatically add the user to the post when it's saved.
        static::creating(function ($post) { // <--
            if (auth()->user()) {
                // $user->user_id = auth()->id();
                $post->user_id = auth()->id(); // should be this
            }
        });

Thanks!

okeworo commented 2 years ago

Thanks for sharing this. I got stucked with likes_count returning null on int

okeworo commented 2 years ago

TypeError Cannot assign null to property App\Http\Livewire\Like::$count of type int (View: C:\Users\SEUN\Documents\GitHub\ANKRAZ\resources\views\frontend\show.blade.php)

?php

namespace App\Http\Livewire;

use App\Models\Store;

use Livewire\Component;

class Like extends Component

{

public Store $post;

public int $count;

public function mount(Store $post)

{

    $this->post = $post;

    $this->count = $post->likes_count;

}

public function like(): void

{

    if ($this->post->isLiked()) {

        $this->post->removeLike();

        $this->count--;

    } elseif (auth()->user()) {

        $this->post->likes()->create([

            'user_id' => auth()->id(),

        ]);

        $this->count++;

    } elseif (($ip = request()->ip()) && ($userAgent = request()->userAgent())) {
dink commented 2 years ago

Thanks for sharing this, but I'm having trouble following the tutorial and getting all sorts of errors along the way. Does anyone have a working example in a demo app I can look at? I need to see the complete models.

Thanks

dink commented 2 years ago

Where is the 'post_id' param being passed from and where is it called? I'm getting a Column 'post_id' cannot be null. Am I missing something?

motoslam commented 2 years ago

If you get a "null" post_id or encounter a similar problem, do not forget to pass the parameter when calling the component:

<livewire:show-post :post="$post">

Alternatively, this is how you can pass in parameters using the Blade directive.

@livewire('show-post', ['post' => $post])
daxdevd commented 1 year ago

Hello, Can you please provide a link of github where this code exists, because this is not working for me. Also can you please let me know is there any other link from where you have taken reference. Hope for the early response. Thanks

daxdevd commented 1 year ago

More, did you have used any js or any ajax call to execute frontend "like" method?