Open utterances-bot opened 3 years ago
That was very useful. Thanks so much
good, but user agent can be longer than 255 characters. better use md5($useragent)
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
}
});
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 assignuser_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!
Thanks for sharing this. I got stucked with likes_count returning null on int
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())) {
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
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?
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])
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
More, did you have used any js or any ajax call to execute frontend "like" method?
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