Creating a like/dislike system in Laravel is a great way to learn more about how this popular PHP framework works. In this tutorial, we’ll walk through building a simple like/dislike system for posts. We’ll cover the basics and provide easy-to-follow code examples.
Prerequisites
Before we start, make sure you have the following:
- PHP and Composer installed on your machine.
- A Laravel project set up. If you don’t have one, you can create a new Laravel project by running:
composer create-project --prefer-dist laravel/laravel like-dislike-system
- A database (MySQL, SQLite, etc.) set up and connected to your Laravel project.
Step 1: Create Models and Migrations
First, we need to create the necessary models and database tables for our posts and likes/dislikes.
Create Post Model and Migration
Run the following command to create a Post
model and its migration:
php artisan make:model Post -m
Update the create_posts_table
migration file (database/migrations/xxxx_xx_xx_create_posts_table.php
) to include the columns we need:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
Create LikeDislike Model and Migration
Run the following command to create a LikeDislike
model and its migration:
php artisan make:model LikeDislike -m
Update the create_like_dislikes_table
migration file (database/migrations/xxxx_xx_xx_create_like_dislikes_table.php
) to include the columns we need:
public function up()
{
Schema::create('like_dislikes', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->onDelete('cascade');
$table->boolean('is_like');
$table->timestamps();
});
}
Now, run the migrations to create the tables in your database:
php artisan migrate
Step 2: Define Relationships in Models
Next, we need to define the relationships between our models.
Post Model
Update the Post
model (app/Models/Post.php
) to include the relationship with LikeDislike
:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = ['title', 'content'];
public function likeDislikes()
{
return $this->hasMany(LikeDislike::class);
}
}
LikeDislike Model
Update the LikeDislike
model (app/Models/LikeDislike.php
) to include the relationship with Post
:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LikeDislike extends Model
{
use HasFactory;
protected $fillable = ['post_id', 'is_like'];
public function post()
{
return $this->belongsTo(Post::class);
}
}
Step 3: Create Routes and Controller
We’ll create a controller to handle the logic for liking and disliking posts, and then define the necessary routes.
Create Controller
Run the following command to create a LikeDislikeController
:
php artisan make:controller LikeDislikeController
Update the LikeDislikeController
(app/Http/Controllers/LikeDislikeController.php
) with the following code:
namespace App\Http\Controllers;
use App\Models\LikeDislike;
use App\Models\Post;
use Illuminate\Http\Request;
class LikeDislikeController extends Controller
{
public function like(Post $post)
{
$post->likeDislikes()->create(['is_like' => true]);
return back();
}
public function dislike(Post $post)
{
$post->likeDislikes()->create(['is_like' => false]);
return back();
}
}
Define Routes
Update the web.php
routes file (routes/web.php
) to include the routes for liking and disliking posts:
use App\Http\Controllers\LikeDislikeController;
Route::get('/posts/{post}/like', [LikeDislikeController::class, 'like'])->name('posts.like');
Route::get('/posts/{post}/dislike', [LikeDislikeController::class, 'dislike'])->name('posts.dislike');
Step 4: Create Views
Finally, we’ll create views to display our posts and the like/dislike buttons.
Create Post View
Create a posts.blade.php
file in the resources/views
directory with the following code:
<!DOCTYPE html>
<html>
<head>
<title>Posts</title>
</head>
<body>
<h1>Posts</h1>
@foreach($posts as $post)
<div>
<h2>{{ $post->title }}</h2>
<p>{{ $post->content }}</p>
<a href="{{ route('posts.like', $post) }}">Like</a>
<a href="{{ route('posts.dislike', $post) }}">Dislike</a>
<p>Likes: {{ $post->likeDislikes->where('is_like', true)->count() }}</p>
<p>Dislikes: {{ $post->likeDislikes->where('is_like', false)->count() }}</p>
</div>
@endforeach
</body>
</html>
Display Posts in a Controller
Update the PostController
to pass the posts to the view. If you don’t have a PostController
, create one:
php artisan make:controller PostController
Update the PostController
(app/Http/Controllers/PostController.php
) with the following code:
namespace App\Http\Controllers;
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$posts = Post::with('likeDislikes')->get();
return view('posts', compact('posts'));
}
}
Add a route to display the posts in the web.php
file:
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
Conclusion
That’s it! You now have a basic like/dislike system in Laravel. You can expand this system by adding user authentication, AJAX requests for better user experience, and more. This tutorial covers the essentials and should give you a good starting point for building more complex features in your Laravel application. Happy coding!
- Check our tools small Tools
- Check our tools website Word count