Issues (14)

src/Http/Livewire/Like.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Usamamuneerchaudhary\Commentify\Http\Livewire;
4
5
6
use Illuminate\Contracts\View\Factory;
7
use Illuminate\Contracts\View\View;
8
use Illuminate\Foundation\Application;
9
use Livewire\Component;
10
11
class Like extends Component
12
{
13
14
    public $comment;
15
    public $count;
16
17
18
    public function mount(\Usamamuneerchaudhary\Commentify\Models\Comment $comment): void
19
    {
20
        $this->comment = $comment;
21
        $this->count = $comment->likes_count;
0 ignored issues
show
The property likes_count does not seem to exist on Usamamuneerchaudhary\Commentify\Models\Comment. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
22
    }
23
24
    public function like(): void
25
    {
26
        $ip = request()->ip();
27
        $userAgent = request()->userAgent();
28
        if ($this->comment->isLiked()) {
29
            $this->comment->removeLike();
30
31
            $this->count--;
32
        } elseif (auth()->user()) {
33
            $this->comment->likes()->create([
34
                'user_id' => auth()->id(),
35
            ]);
36
37
            $this->count++;
38
        } elseif ($ip && $userAgent) {
39
            $this->comment->likes()->create([
40
                'ip' => $ip,
41
                'user_agent' => $userAgent,
42
            ]);
43
44
            $this->count++;
45
        }
46
    }
47
48
    /**
49
     * @return Factory|Application|View|\Illuminate\Contracts\Foundation\Application|null
50
     */
51
    public function render(
52
    ): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|null
53
    {
54
        return view('commentify::livewire.like');
55
    }
56
57
}
58