Skip to content

Commit 5e251ad

Browse files
authored
Merge pull request #30 from DanielRTRD/user-guest-like-test
User guest like
2 parents f8429bf + 50983e9 commit 5e251ad

File tree

10 files changed

+126
-119
lines changed

10 files changed

+126
-119
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/public/storage
44
/storage/*.key
55
/vendor
6+
/.idea
67
.env
78
.env.backup
89
.phpunit.result.cache

app/Http/Livewire/Like.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,30 @@
33
namespace App\Http\Livewire;
44

55
use App\Models\Post;
6+
use App\Models\User;
67
use Livewire\Component;
78

89
class Like extends Component
910
{
1011
public Post $post;
1112
public int $count;
13+
public $user;
1214

1315
public function mount(Post $post)
1416
{
1517
$this->post = $post;
16-
$this->count = $post->likes_count;
18+
$this->count = $post->likes()->count();
19+
$this->user = auth()->check() ? auth()->user() : null;
1720
}
1821

1922
public function like()
2023
{
21-
if ($this->post->isLiked()) {
22-
$this->post->removeLike();
23-
$this->count--;
24-
} elseif (auth()->user()) {
25-
$this->post->likes()->create([
26-
'user_id' => auth()->id(),
27-
]);
28-
$this->count++;
29-
} elseif (($ip = request()->ip()) && ($userAgent = request()->userAgent())) {
30-
$this->post->likes()->create([
31-
'ip' => $ip,
32-
'user_agent' => $userAgent,
33-
]);
34-
$this->count++;
24+
if ($this->post->hasLiked($this->user)) {
25+
$this->post->dislike($this->user);
26+
$this->count = $this->post->likes()->count();
27+
} else {
28+
$this->post->like($this->user);
29+
$this->count = $this->post->likes()->count();
3530
}
3631
}
3732

app/Models/Post.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
use CyrildeWit\EloquentViewable\Contracts\Viewable;
1010
use CyrildeWit\EloquentViewable\InteractsWithViews;
1111
use Illuminate\Database\Eloquent\Factories\HasFactory;
12-
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;
12+
use Kilobyteno\LaravelUserGuestLike\Traits\HasUserGuestLike;
1313

1414
class Post extends Model implements Viewable
1515
{
1616
use HasFactory;
1717
use InteractsWithViews;
1818
use SoftDeletes;
1919
use HasTags;
20+
use HasUserGuestLike;
2021

2122
protected $removeViewsOnDelete = true;
2223

@@ -70,37 +71,6 @@ public function user()
7071
return $this->belongsTo(User::class);
7172
}
7273

73-
public function likes()
74-
{
75-
return $this->hasMany(PostLike::class);
76-
}
77-
78-
public function isLiked()
79-
{
80-
if (auth()->user()) {
81-
return auth()->user()->likes()->forPost($this)->count();
82-
}
83-
84-
if (($ip = request()->ip()) && ($userAgent = request()->userAgent())) {
85-
return $this->likes()->forIp($ip)->forUserAgent($userAgent)->count();
86-
}
87-
88-
return false;
89-
}
90-
91-
public function removeLike()
92-
{
93-
if (auth()->user()) {
94-
return auth()->user()->likes()->forPost($this)->delete();
95-
}
96-
97-
if (($ip = request()->ip()) && ($userAgent = request()->userAgent())) {
98-
return $this->likes()->forIp($ip)->forUserAgent($userAgent)->delete();
99-
}
100-
101-
return false;
102-
}
103-
10474
public function scopeIsPublished($query)
10575
{
10676
return $query->where('draft', false)->where('published_at', '<=', now());

app/Models/PostLike.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "laravel/laravel",
2+
"name": "danielrtrd/daniel.rtrd.no",
33
"type": "project",
4-
"description": "The Laravel Framework.",
4+
"description": "Just my personal website",
55
"keywords": ["framework", "laravel"],
66
"license": "MIT",
77
"require": {
@@ -12,6 +12,7 @@
1212
"fideloper/proxy": "^4.4",
1313
"fruitcake/laravel-cors": "^2.0",
1414
"guzzlehttp/guzzle": "^7.0.1",
15+
"kilobyteno/laravel-user-guest-like": "^0.0.1",
1516
"laravel/framework": "^8.40",
1617
"laravel/jetstream": "^2.3",
1718
"laravel/sanctum": "^2.6",

composer.lock

Lines changed: 72 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/user-guest-like.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
return [
4+
5+
// Let guests like a model
6+
'guest_like_enabled' => true,
7+
8+
// Save IP and user agent to database
9+
'user_tracking_enabled' => false,
10+
11+
];

database/migrations/2021_12_31_122354_create_post_likes_table.php

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('user_guest_likes', function (Blueprint $table) {
12+
$table->id();
13+
$table->morphs('model');
14+
$table->nullableMorphs('author');
15+
$table->ipAddress('ip')->nullable();
16+
$table->string('user_agent')->nullable();
17+
$table->timestamps();
18+
});
19+
}
20+
21+
public function down()
22+
{
23+
Schema::dropIfExists('user_guest_likes');
24+
}
25+
};

resources/views/livewire/like.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<span class="flex items-center w-full mt-12 text-3xl align-center">
2-
<button wire:click="like" wire:loading.attr="disabled" class="flex mx-auto {{ $post->isLiked() ? 'text-orange-500 hover:text-orange-400' : 'text-gray-400 hover:text-orange-300' }} focus:outline-none focus:ring-0 disabled:text-gray-700">
3-
@if($post->isLiked())
2+
<button wire:click="like" wire:loading.attr="disabled" class="flex mx-auto {{ $post->hasLiked($user) ? 'text-orange-500 hover:text-orange-400' : 'text-gray-400 hover:text-orange-300' }} focus:outline-none focus:ring-0 disabled:text-gray-700">
3+
@if($post->hasLiked($user))
44
<svg xmlns="http://www.w3.org/2000/svg" class="inline-block w-8 h-8" viewBox="0 0 20 20" fill="currentColor">
55
<path fill-rule="evenodd" d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z" clip-rule="evenodd" />
66
</svg>

0 commit comments

Comments
 (0)