In Laravel 11, the like and dislike system allows users to express their likes or dislikes for products.
Let’s start to implement like and dislike system:
Step 1 – Create Model and Migration
Run the following commands to create the Post and Like models and migration files:
php artisan make:Model Post -m
php artisan make:Model Like -m
Edit database/migrations/_create_posts_table.php
file, and add the following code in it:
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
Edit database/migrations/_create_likes_table.php
file, and add the following code in it:
public function up(): void
{
Schema::create('likes', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->onDelete('cascade');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->boolean('like'); // true for like, false for dislike
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('likes');
}
Edit app/Models/Post.php
file, and implement relationship for like and dislike:
use HasFactory;
protected $fillable = ['title', 'body'];
/**
* Write code on Method
*
* @return response()
*/
public function likes()
{
return $this->hasMany(Like::class)->where('like', true);
}
/**
* Write code on Method
*
* @return response()
*/
public function dislikes()
{
return $this->hasMany(Like::class)->where('like', false);
}
Edit app/Models/Like.php
file, and implement relationship between users and posts table:
use HasFactory;
protected $fillable = ['post_id', 'user_id', 'like'];
/**
* Write code on Method
*
* @return response()
*/
public function post()
{
return $this->belongsTo(Post::class);
}
/**
* Write code on Method
*
* @return response()
*/
public function user()
{
return $this->belongsTo(User::class);
}
Add like and dislike relationship query in app/Models/User.php
file:
/**
* Write code on Method
*
* @return response()
*/
public function likes()
{
return $this->hasMany(Like::class);
}
/**
* Write code on Method
*
* @return response()
*/
public function hasLiked($postId)
{
return $this->likes()->where('post_id', $postId)->where('like', true)->exists();
}
/**
* Write code on Method
*
* @return response()
*/
public function hasDisliked($postId)
{
return $this->likes()->where('post_id', $postId)->where('like', false)->exists();
}
/**
* Write code on Method
*
* @return response()
*/
public function toggleLikeDislike($postId, $like)
{
// Check if the like/dislike already exists
$existingLike = $this->likes()->where('post_id', $postId)->first();
if ($existingLike) {
if ($existingLike->like == $like) {
$existingLike->delete();
return [
'hasLiked' => false,
'hasDisliked' => false
];
} else {
$existingLike->update(['like' => $like]);
}
} else {
$this->likes()->create([
'post_id' => $postId,
'like' => $like,
]);
}
return [
'hasLiked' => $this->hasLiked($postId),
'hasDisliked' => $this->hasDisliked($postId)
];
}
Run the migration command to create tables in database using migration file:
php artisan migrate
Step 2 – Add Dummy Data
Run the following command to create seeder file:
php artisan make:seeder CreateDummyPost
Edit the database/seeders/CreateDummyPost.php
file and add the following code in it:
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Post;
class CreateDummyPost extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$posts = [
[
'title' => 'Laravel Product CRUD Tutorial',
'body' => 'Step by Step Laravel Product CRUD Tutorial'
],
[
'title' => 'Laravel Image Upload Example',
'body' => 'Step by Step Laravel Image Upload Example'
],
[
'title' => 'Laravel File Upload Example',
'body' => 'Step by Step Laravel File Upload Example'
],
[
'title' => 'Laravel Cron Job Example',
'body' => 'Step by Step Laravel Cron Job Example'
],
[
'title' => 'Laravel Send Email Example',
'body' => 'Step by Step Laravel Send Email Example'
],
[
'title' => 'Laravel CRUD with Image Upload',
'body' => 'Step by Step Laravel CRUD with Image Upload'
],
[
'title' => 'Laravel Ajax CRUD with Image Upload',
'body' => 'Step by Step Laravel Ajax CRUD with Image Upload'
],
[
'title' => 'Laravel Ajax CRUD with Image Upload',
'body' => 'Step by Step Laravel Ajax CRUD with Image Upload'
]
];
foreach ($posts as $key => $value) {
Post::create([
'title' => $value['title'],
'body' => $value['body'],
]);
}
}
}
Run the seeder to add dummy data in database table:
php artisan db:seed --class=CreateDummyPost
Step 3 – Set Up Auth
Run the following command to install and set up bootstrap auth:
composer require laravel/ui
php artisan ui bootstrap --auth
npm install
npm run build
Step 4 – Create Routes
Edit routes/web.php
file, and create the like dislike route in it:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::middleware('auth')->group(function () {
Route::get('posts', [PostController::class, 'index'])->name('posts.index');
Route::post('posts/ajax-like-dislike', [PostController::class, 'ajaxLike'])->name('posts.ajax.like.dislike');
});
Step 5 – Create Controller
Run the following command to create a controller file:
php artisan make:controller PostController
Edit app/Http/PostController.php
file, and implement like dislike logic in it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::get();
return view('posts', compact('posts'));
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function ajaxLike(Request $request)
{
$post = Post::find($request->id);
$response = auth()->user()->toggleLikeDislike($post->id, $request->like);
return response()->json(['success' => $response]);
}
}
Step 6 – Create View
Create view named posts.blade.php
file in resources/views
directory:
@extends('layouts.app')
@section('style')
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style type="text/css">
i{
cursor: pointer;
}
</style>
@endsection
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">{{ __('Posts List') }}</div>
<div class="card-body">
<div class="row">
@foreach($posts as $post)
<div class="col-md-3">
<div class="card mt-2" style="width: 18rem;">
<img src="https://picsum.photos/id/0/367/267" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{ $post->title }}</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<div class="like-box">
<i id="like-{{ $post->id }}"
data-post-id="{{ $post->id }}"
class="like fa-thumbs-up {{ auth()->user()->hasLiked($post->id) ? 'fa-solid' : 'fa-regular' }}"></i>
<span class="like-count">{{ $post->likes->count() }}</span>
<i id="like-{{ $post->id }}"
data-post-id="{{ $post->id }}"
class="dislike fa-thumbs-down {{ auth()->user()->hasDisliked($post->id) ? 'fa-solid' : 'fa-regular' }}"></i>
<span class="dislike-count">{{ $post->dislikes->count() }}</span>
</div>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('script')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" referrerpolicy="no-referrer"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.like-box i').click(function(){
var id = $(this).attr('data-post-id');
var boxObj = $(this).parent('div');
var c = $(this).parent('div').find('span').text();
var like = $(this).hasClass('like') ? 1 : 0;
$.ajax({
type:'POST',
url: "{{ route('posts.ajax.like.dislike') }}",
data:{ id:id, like:like },
success:function(data){
if (data.success.hasLiked == true) {
if($(boxObj).find(".dislike").hasClass("fa-solid")){
var dislikes = $(boxObj).find(".dislike-count").text();
$(boxObj).find(".dislike-count").text(parseInt(dislikes)-1);
}
$(boxObj).find(".like").removeClass("fa-regular");
$(boxObj).find(".like").addClass("fa-solid");
$(boxObj).find(".dislike").removeClass("fa-solid");
$(boxObj).find(".dislike").addClass("fa-regular");
var likes = $(boxObj).find(".like-count").text();
$(boxObj).find(".like-count").text(parseInt(likes)+1);
} else if(data.success.hasDisliked == true){
if($(boxObj).find(".like").hasClass("fa-solid")){
var likes = $(boxObj).find(".like-count").text();
$(boxObj).find(".like-count").text(parseInt(likes)-1);
}
$(boxObj).find(".like").removeClass("fa-solid");
$(boxObj).find(".like").addClass("fa-regular");
$(boxObj).find(".dislike").removeClass("fa-regular");
$(boxObj).find(".dislike").addClass("fa-solid");
var dislike = $(boxObj).find(".dislike-count").text();
$(boxObj).find(".dislike-count").text(parseInt(dislike)+1);
} else {
if($(boxObj).find(".dislike").hasClass("fa-solid")){
var dislikes = $(boxObj).find(".dislike-count").text();
$(boxObj).find(".dislike-count").text(parseInt(dislikes)-1);
}
if($(boxObj).find(".like").hasClass("fa-solid")){
var likes = $(boxObj).find(".like-count").text();
$(boxObj).find(".like-count").text(parseInt(likes)-1);
}
$(boxObj).find(".like").removeClass("fa-solid");
$(boxObj).find(".like").addClass("fa-regular");
$(boxObj).find(".dislike").removeClass("fa-solid");
$(boxObj).find(".dislike").addClass("fa-regular");
}
}
});
});
});
</script>
@endsection
Edit resources/views/layouts/app.blade.php
file, and update the following code in it:
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Laravel 11 Like Dislike System - ItcodStuff.com</title>
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.bunny.net">
<link href="https://fonts.bunny.net/css?family=Nunito" rel="stylesheet">
<!-- Scripts -->
@vite(['resources/sass/app.scss', 'resources/js/app.js'])
@yield('style')
</head>
<body>
<div id="app">
<nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="{{ url('/') }}">
Laravel Like DisLike System Example
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<!-- Left Side Of Navbar -->
<ul class="navbar-nav me-auto">
</ul>
<!-- Right Side Of Navbar -->
<ul class="navbar-nav ms-auto">
<!-- Authentication Links -->
@guest
@if (Route::has('login'))
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
@endif
@if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
@endif
@else
<li class="nav-item">
<a class="nav-link" href="{{ route('posts.index') }}">{{ __('Posts') }}</a>
</li>
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }}
</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
@csrf
</form>
</div>
</li>
@endguest
</ul>
</div>
</div>
</nav>
<main class="py-4">
@yield('content')
</main>
</div>
</body>
@yield('script')
</html>
Step 7 – Test This Application
Run the following command to start application server:
php artisan serve
Open browser and type http://localhost:8000/
url on browser to test this application.