In this tutorial you will learn about the Laravel 7 Vue JS Like Dislike Example and its application with practical example.
In this Laravel 7 Vue JS Like Dislike Example Tutorial I’ll show you how to implement Like Dislike with Vue Js in laravel. In this tutorial you will learn to create Like Dislike with Vue Js in laravel.
- Laravel 7 Vue JS Like Dislike Example
- Step 1: Download Laravel Fresh App
- Step 2: Add Database Details
- Step 3: Run Make auth Command
- Step 4: Create Model And Migration
- Step 5: NPM Configuration
- Step 6: Add Routes
- Step 7: Create Controller By Command
- Step 8: Create Vue Component
- Step 9: Create Blade Views And Initialize Vue Components
- Step 10: Run Development Server
Laravel 7 Vue JS Like Dislike Example
- Step 1: Download Laravel Fresh App
- Step 2: Add Database Detail
- Step 3: Run Make auth Command
- Step 4: Create Model And Migration
- Step 5: NPM Configuration
- Step 6: Add Routes
- Step 7: Create Controller By Command
- Step 8: Create Vue Component
- Step 9: Create Blade Views And Initialize Vue Components
- Step 10: Run Development Server
Step 1: Download Laravel Fresh App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Add Database Details
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3: Run Make auth Command
1 2 3 4 5 |
cd blog composer require laravel/ui --dev php artisan ui vue --auth |
Step 4: Create Model And Migration
Now, in this step we will create model and migration file for form. Please run the following command:
1 |
php artisan make:model Post -fm |
This command will create one model name post.php and also create one migration file for the posts table. Now open create_postss_table.php migration file from database>migrations and replace up() function with following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('title'); $table->string('slug'); $table->unsignedBigInteger('user_id'); $table->unsignedBigInteger('like')->default(0); $table->unsignedBigInteger('dislike')->default(0); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Next, Navigate to app/Post.php and update the following code into your Post.php model as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $guarded = []; public function getRouteKeyName() { return 'slug'; } } |
Next, Navigate to database/factories and open PostFactory.php. Then update the following code into it as follow:
1 2 3 4 5 6 7 8 9 10 11 12 |
/** @var \Illuminate\Database\Eloquent\Factory $factory */ use App\Post; use Faker\Generator as Faker; $factory->define(Post::class, function (Faker $faker) { return [ 'title' => $faker->sentence, 'slug' => \Str::slug($faker->sentence), 'user_id' => 1 ]; }); |
and then run the following commad to generate fake data using faker as follow:
1 2 3 4 |
php artisan tinker //and then factory(\App\Post::class,20)->create() exit |
Step 5: NPM Configuration
1 |
php artisan preset vue |
Install all Vue dependencies:
1 |
npm install |
Step 6: Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 |
//posts listing and view routes Route::get('post','PostController@index'); Route::get('post/{slug?}','PostController@show')->name('post'); //like routes Route::post('/like','PostController@getlike'); Route::post('/like/{id}','PostController@like'); //dislike routes Route::post('/dislike','PostController@getDislike'); Route::post('/dislike/{id}','PostController@dislike'); |
Step 7: Create Controller By Command
Now, lets create a controller named PostController using command given below –
1 |
php artisan make:controller PostController |
After that, go to app\Http\Controllers and open PostController.php file. Then update the following code into your PostController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
namespace App\Http\Controllers; use App\Post; use Facades\App\Repository\Posts; use Illuminate\Http\Request; class PostController extends Controller { public function index() { $posts = Post::latest()->get(); return view('posts',['posts' => $posts ]); } public function show(Post $slug) { return view('single-post',['post' => $slug ]); } public function getlike(Request $request) { $post = Post::find($request->post); return response()->json([ 'post'=>$post, ]); } public function like(Request $request) { $post = Post::find($request->post); $value = $post->like; $post->like = $value+1; $post->save(); return response()->json([ 'message'=>'Thanks', ]); } public function getDislike(Request $request) { $post = Post::find($request->post); return response()->json([ 'post'=>$post, ]); } public function dislike(Request $request) { $post = Post::find($request->post); $value = $post->dislike; $post->dislike = $value+1; $post->save(); return response()->json([ 'message'=>'Thanks', ]); } } |
Step 8: Create Vue Component
Next step, go to resources/assets/js/components folder and create a files called LikeComponent.vue and DisLikeComponent.vue.
Now, update the following code into your LikeComponent.vue components file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<template> <div class="container"> <p id="success"></p> <a href="http://"><i @click.prevent="likePost" class="fa fa-thumbs-up" aria-hidden="true"></i>({{ totallike }})</a> </div> </template> <script> export default { props:['post'], data(){ return { totallike:'', } }, methods:{ likePost(){ axios.post('/like/'+this.post,{post:this.post}) .then(response =>{ this.getlike() $('#success').html(response.data.message) }) .catch() }, getlike(){ axios.post('/like',{post:this.post}) .then(response =>{ console.log(response.data.post.like) this.totallike = response.data.post.like }) } }, mounted() { this.getlike() } } </script> |
Next, update the following code into DisLikeComponent.vue file as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<template> <div class="container"> <p id="success"></p> <a href="http://"><i @click.prevent="disLikePost" class="fa fa-thumbs-down" aria-hidden="true"></i>({{ totalDislike }})</a> </div> </template> <script> export default { props:['post'], data(){ return { totalDislike:'', } }, methods:{ disLikePost(){ axios.post('/dislike/'+this.post,{post:this.post}) .then(response =>{ this.getDislike() $('#success').html(response.data.message) }) .catch() }, getDislike(){ axios.post('/dislike',{post:this.post}) .then(response =>{ console.log(response.data.post.dislike) this.totalDislike = response.data.post.dislike }) } }, mounted() { this.getDislike() } } </script> |
Now open resources/assets/js/app.js and include the LikeComponent.vue and DisLikeComponent.vue component as follow:
1 2 3 4 5 6 7 8 9 10 11 |
require('./bootstrap'); window.Vue = require('vue'); Vue.component('like-component', require('./components/LikeComponent.vue').default); Vue.component('dis-like-component', require('./components/DisLikeComponent.vue')); const app = new Vue({ el: '#app', }); |
Step 9: Create Blade Views And Initialize Vue Components
In this step, navigate to resources/views and create one folder named layouts. Inside this folder create one blade view file named app.blade.php file.
Next, Navigate to resources/views/layouts and open app.blade.php file. Then update the following code into your app.blade.php file as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>Laravel Vue JS Like Dislike Example</title> <script src="{{ asset('js/app.js') }}" defer></script> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> @stack('fontawesome') </head> <body> <div id="app"> <main class="py-4"> @yield('content') </main> </div> </body> </html> |
Next, Navigate to resources/views/ and create one file name posts.blade.php. Then update the following code into your posts.blade.php file as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Post Lists</div> <div class="card-body"> <ul> @foreach ($posts as $item) <a href="{{ route('post',$item->slug) }}"><li>{{ $item->title }}</li></a> @endforeach </ul> </div> </div> </div> </div> </div> @endsection |
Again, Navigate to resources/views/ and create one file name single-post.blade.php. Then update the following code into your single-post.blade.php file as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@extends('layouts.app') @push('fontawesome') <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> @endpush @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Post Detail</div> <div class="card-body"> <ul> <li>{{ $post->title }}</li> <like-component :post="{{ $post->id }}"></like-component> <dis-like-component :post="{{ $post->id }}"></dis-like-component> </ul> </div> </div> </div> </div> </div> @endsection |
Step 10: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 2 3 4 5 |
npm run dev or npm run watch |