I need any user to be able to like it every 24 hours
I wrote a function for this
const LIKE_HEART = 'like_heart';
const LIKE_FINGER = 'like_finger';
public static $types = [self::LIKE_HEART, self::LIKE_FINGER];
public function setLikes() {
$likes = Cookie::get($types);
$hours = 24;
if ($likes) {
self::where('id', $article->id)
->where('updated_at', '<', Carbon::now()->subHours($hours))
->increment($types);
}
}
But I have two fields in my database, like_heart
and like_finger
, these should be two types of likes. How can I rewrite my function into a method so that I can only choose one type of like out of two?
An array of the following type must be serialized in cookies:
$r = [
'1' => [
'like_heart' => '2021-09-28 22:02:01',
'like_finger' => '2021-11-28 11:12:34',
],
'2' => [
'like_finger' => '2021-11-28 11:12:34',
],
];
where 1, 2 is the article ID. Date - the date the like was added by current users The current date is compared with the date in this cookie for a specific article, and if it is less than 24 hours old or missing, add +1 to the corresponding like in the article and add / change information in the like.
article.blade
<div class="blog-wrapper">
<div class="container">
<div class="article">
<p><b>{!! $article->title !!}</b></p>
<p><b>{!! $article->subtitle !!}</b></p>
<picture>
<source srcset="{{ $article->image_list_mobile }}" media="(max-width: 576px)" alt="{{ $article->image_mobile_alt }}" title="{{ $article->image_mobile_title }}">
<source srcset="{{ $article->image_list }}" alt="{{ $article->image_alt }}" title="{{ $article->image_title }}">
<img srcset="{{ $article->image_list }}" alt="{{ $article->image_alt }}" title="{{ $article->image_title }}">
</picture>
<p><b>{{ date('d F Y', strtotime($article->published_at)) }}</b></p>
<p><b>{{ $article->getTotalViews() }} Views</b></p>
<p><b>{{ $allArticleCommentsCount }} Comments</b></p>
</div>
<a href="/article/{{ $article->id }}/like?type=like_heart" class="btn btn-primary">Like Heart</a>
<a href="/article/{{ $article->id }}/like?type=like_finger" class="btn btn-primary">Like Finger</a>
<div class="comments">
<div class="recommend-title"><p><b>Comments ({{ $allArticleCommentsCount }})</b></p></div>
@foreach($article_comments as $article_comment)
<p><b>{!! $article_comment->name !!}</b></p>
<p><b>{!! $article_comment->text !!}</b></p>
<p><b>{{ date('d F Y', strtotime($article_comment->date)) }}</b></p>
@endforeach
</div>
</div>
</div>
controller
public function index(Request $request, $slug)
{
$article = Article::where('slug', $slug)->first();
if(!$article){
return abort(404);
}
$viewed = Session::get('viewed_article', []);
if (!in_array($article->id, $viewed)) {
$article->increment('views');
Session::push('viewed_article', $article->id);
}
$allArticleCommentsCount = ArticleComment::where('article_id', $article->id)->count();
$article_comments = ArticleComment::where('article_id', $article->id)->get();
return view('article', compact('article', 'article_comments', 'allArticleCommentsCount'));
}
public function postLike() {
if ($like = request('like')) {
$articleId = request('article_id');
if (User::hasLikedToday($articleId, $like)) {
return response()
->json([
'message' => 'You have already liked the Article #'.$articleId.' with '.$like.'.',
]);
}
$cookie = User::setLikeCookie($articleId, $like);
return response()
->json([
'message' => 'Liked the Article #'.$articleId.' with '.$like.'.',
'cookie_json' => $cookie->getValue(),
])
->withCookie($cookie);
}
}
Article model
class User extends Authenticatable
{
// ...
public static function hasLikedToday($articleId, string $type)
{
$articleLikesJson = \Cookie::get('article_likes', '{}');
$articleLikes = json_decode($articleLikesJson, true);
// Check if there are any likes for this article
if (! array_key_exists($articleId, $articleLikes)) {
return false;
}
// Check if there are any likes with the given type
if (! array_key_exists($type, $articleLikes[$articleId])) {
return false;
}
$likeDatetime = Carbon::createFromFormat('Y-m-d H:i:s', $articleLikes[$articleId][$type]);
return ! $likeDatetime->addDay()->lt(now());
}
public static function setLikeCookie($articleId, string $type)
{
// Initialize the cookie default
$articleLikesJson = \Cookie::get('article_likes', '[]');
$articleLikes = json_decode($articleLikesJson, true);
// Update the selected articles type
$articleLikes[$articleId][$type] = today()->format('Y-m-d H:i:s');
$articleLikesJson = json_encode($articleLikes);
return cookie()->forever('article_likes', $articleLikesJson);
}
}
route
Route::get('/article', function () {
$articleLikesJson = \Cookie::get('article_likes', '{}');
return view('article')->with([
'articleLikesJson' => $articleLikesJson,
]);
});
Route::get('article/{id}/like', 'App\Http\Controllers\ArticleController@postLike');