I have functions that I use in my Article model, they add likes to cookies for a specific article and record the time
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); } The php.blade page itself has buttons
<a href="/article/{{ $article->id }}/like?type=heart" class="btn btn-primary">Like Heart</a> <a href="/article/{{ $article->id }}/like?type=finger" class="btn btn-primary">Like Finger</a> Here are the routes web.php
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'); And the postLike() function itself in the controller
public function postLike($id) { $article = Article::find($id); $like = request('like'); if ($article->hasLikedToday($article->id, $like)) { return response() ->json([ 'message' => 'You have already liked the Article #'.$article->id.' with '.$like.'.', ]); } $cookie = $article->setLikeCookie($article->id, $like); $article->increment('like_{$like}'); return response() ->json([ 'message' => 'Liked the Article #'.$article->id.' with '.$like.'.', 'cookie_json' => $cookie->getValue(), ]) ->withCookie($cookie); } In general, what is the problem, I have 2 types of likes that can be seen in php.blade, and the problem is to pass the choice of the type of like to the postLike() function, if in my function instead of $like I write 'heart', then everything will be work, but I need to determine which type we choose (heart or finger), tell me how this can be done?