Today we are going to build a Like and Dislike system. To implement this application, we use php framework Laravel and Progressive JavaScript Framework Vue.js.
Prerequisites
Laravel
To Continue with this tutorial, you should have installed Laravel 8 in your pc. If you are still not installed Laravel in your machine you can configure it from here.
Vue.jS
You can refer the official Vue.js documentation from here.
Setting up the Project
First you need to create a new laravel project by running below command in your terminal
Then navigate to your project directory by using below command in your terminal
1
cd laravel-vue-like-dislike-system
Setting up Database
To setup up Database for our project, open the application using your favourite text editor and and then navigate to .env file in it and then change below section acording to your Database settings:
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;
class Blog extends Model { use HasFactory; protected $fillable = [ 'post_title', 'description' ]; public function getRouteKeyName() { return 'post_slug'; } }
Afterwards, we need to do database migration:
1
php artisan migrate
Generate fake data
For testing our application wee need to add some fake data. To do it add below code to your BlogSeeder.php:
class BlogSeeder extends Seeder { public function run(){ Blog::create([ 'post_title' => $this->faker->unique()->sentence, 'description' => $this->faker->paragraph, 'post_slug' => $this->faker->unique()->sentence, 'user_id' => 1, 'created_at' => \Carbon\Carbon::now(), 'updated_at' => \Carbon\Carbon::now(), ]); } }
Then add below code for your DatabaseSeeder.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder; use App\Models\Blog;
class DatabaseSeeder extends Seeder { public function run() { \App\Models\Blog::factory(10)->create(); } }
After that run below coomand to run seed:
1
php artisan db:seed
Install Vue UI and NPM Dependencies
To install Vue in our application in our Laravel project, run below command:
1
composer require laravel/ui
Then,
1
php artisan ui vue
1
npm install
Create Blog Controller
In this stage we are going to make our BlogController which holds our all logic related to blog section like fetch all blogs, fetch single blog, like and dislike function and more. To generate our controller:
For this application we nned two Vue components. Head over to resources/assets/js/components directory, create two files, name them LikeComponent.vue and DislikeComponent.vue and add below command in LikeComponent.vue:
In this tutorial, we implemented a simple Like and Dislike system using Laravel and Vue.js. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail. You can obtain complete source code for this tutorial from this GitHub repository.