HomeLaravelVideo

Laravel 5.5 AJAX : Create Like & Dislike System using AJAX in Laravel 5.5

Laravel 5.5 Ajax Tutorial : How to create like and dislike system using ajax in laravel 5.5 step by step with example

Laravel 5.7 Tutorial : Datatables API with Example in Laravel 5.7
Laravel 5.5 AJAX Tutorial : Advanced CRUD Operation with Ajax JQuery in Laravel 5.5
Laravel 5 Tutorial : How to Implement DataTables Ajax with yajra datatables
Laravel 5.5 and Ajax tutorial for beginners : How to create simple Like and Dislike app like social medias using Ajax Request in laravel 5.5, this lesson will show you how to build social media likes and dislikes system.

Previews lessons, we have create one project named with laravel admin project, before doing next lessons, you must have this project before, please read How to create Advanced Admin Panel in Laravel 5.5

Video Tutorial Like and Dislike System with Ajax

Just watch this video and don't forget to like, share and subscribe our channel for more awesome videos every day.

Source Code

First, make sure you have installed new laravel project laraveladmin
composer create-project --prefer-dist laravel/laravel laraveladmin

Create Connection

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sectorcode
DB_USERNAME=root
DB_PASSWORD=null

Create Laravel Authentication

php artisan make:auth

Create Model and Migration

php artisan make:model Post -m
php artisan make:model Like -m

Post migration

        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->integer('user_id'); //on user table
        });

Like migration

        Schema::create('likes', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->integer('user_id'); // on user table
            $table->integer('post_id'); // on post table
            $table->boolean('like');
        });

Run Migration

php artisan migrate

Model

Post Model

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Post extends Model
{
    public function user(){
      return $this->belongsTo('AppUser');
    }
    
    public function likes(){
      return $this->belongsTo('AppLike');
    }
}

Like Model

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Like extends Model
{
    public function user(){
      return $this->belongsTo('AppUser');
    }
    public function post(){
      return $this->belongsTo('AppPost');
    }
}

User Model

<?php

namespace App;

use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function likes(){
      return $this->hasMany('AppLike');
    }
    public function post(){
      return $this->hasMany('AppPost');
    }
}

Post Controller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppLike;
use AppPost;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesSession;

class PostController extends Controller
{
    public function index(){
      $posts = Post::orderBy('created_at', 'desc')->get();
        return view('post.index', ['posts' => $posts]);
    }
    public function postLikePost(Request $request)
       {
           $post_id = $request['postId'];
           $is_like = $request['isLike'] === 'true';
           $update = false;
           $post = Post::find($post_id);
           if (!$post) {
               return null;
           }
           $user = Auth::user();
           $like = $user->likes()->where('post_id', $post_id)->first();
           if ($like) {
               $already_like = $like->like;
               $update = true;
               if ($already_like == $is_like) {
                   $like->delete();
                   return null;
               }
           } else {
               $like = new Like();
           }
           $like->like = $is_like;
           $like->user_id = $user->id;
           $like->post_id = $post->id;
           if ($update) {
               $like->update();
           } else {
               $like->save();
           }
           return null;
       }
}

Routes

Route::get('/post', 'PostController@index');
Route::post('/like','PostController@postLikePost')->name('like');

Vews

In resources/views/ create new folder named with "post", and in the post folder we will store all post data into index.blade.php

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Like & Dislike</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
   <div class="container">
     <div class="blog-header">
       <h2>Like & Dislike System</h2>
       <p>Using ajax to like and disklike system in laravel 5.5</p>
       <hr>
     </div>

     <div class="row">
       <div class="col-sm-8">
         <div class="blog-post">
           @foreach ($posts as $post)
             <div class="post" data-postid="{{ $post->id }}">
             <a href="#"><h3>{{$post->title}}</h3></a>
             <h6>Posted on {{$post->created_at}} by {{$post->user->name}}</h6>
             <p>{{$post->body}}</p>
             <div class="interaction">
               <a href="#" class="btn btn-xs btn-warning like">{{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 1 ? 'You like this post' : 'Like' : 'Like'  }}</a> |
               <a href="#" class="btn btn-xs btn-danger like">{{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 0 ? 'You don't like this post' : 'Dislike' : 'Dislike'  }}</a>
            </div>
             <hr>
           </div>
           @endforeach
         </div><!-- /.blog-post -->
         <nav>
           <ul class="pager">
             <li><a href="#">Previous</a></li>
             <li><a href="#">Next</a></li>
           </ul>
         </nav>
       </div><!-- /.blog-main -->
     </div><!-- /.row -->
     <div class="footer">
       <p>Copyright © 2018 <a href="#">Sector Code</a></p>
       <p>
         <a href="#">Back to top</a>
       </p>
     </div>
   </div><!-- /.container -->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="{{ asset('/js/like.js') }}"></script>
    <script>
      var token = '{{ Session::token() }}';
      var urlLike = '{{ route('like') }}';
    </script>
  </body>
</html>

Java Script Files

In Public/Js/ we will create new file named with "like.js"

var postId = 0;
$('.like').on('click', function(event) {
    event.preventDefault();
    postId = event.target.parentNode.parentNode.dataset['postid'];
    var isLike = event.target.previousElementSibling == null;
    $.ajax({
        method: 'POST',
        url: urlLike,
        data: {isLike: isLike, postId: postId, _token: token}
    })
        .done(function() {
            event.target.innerText = isLike ? event.target.innerText == 'Like' ? 'You like this post' : 'Like' : event.target.innerText == 'Dislike' ? 'You don't like this post' : 'Dislike';
            if (isLike) {
                event.target.nextElementSibling.innerText = 'Dislike';
            } else {
                event.target.previousElementSibling.innerText = 'Like';
            }
        });
});

More Laravel Video Tutorial









See you next lessons and happy coding guys ...

Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


COMMENTS

DISQUS
Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: Laravel 5.5 AJAX : Create Like & Dislike System using AJAX in Laravel 5.5
Laravel 5.5 AJAX : Create Like & Dislike System using AJAX in Laravel 5.5
Laravel 5.5 Ajax Tutorial : How to create like and dislike system using ajax in laravel 5.5 step by step with example
https://i.ytimg.com/vi/FKEWlsNmkD0/hqdefault.jpg
https://i.ytimg.com/vi/FKEWlsNmkD0/default.jpg
KODE AJAIB
https://scqq.blogspot.com/2017/12/laravel-55-ajax-create-like-dislike.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2017/12/laravel-55-ajax-create-like-dislike.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy