How to add view count for post in laravel

Solution for How to add view count for post in laravel
is Given Below:

I am performing a Blog application using laravel.. I want to track the count of the post view whenever a user view a particular blog post it should increment only once whether it is a registered user or non-registered users. … And also want to display the most viewed blogs according to the view count.. Can anyone assist with the logic.

/ Table
Schema::create('posts', function(Blueprint $table)
{
    $table->increments('id');
    $table->text('body')
    $table->integer('count');
    ...
    $table->timestamps();
});



public function show($id) 
{
    $post = Post::find($id);

    Post::update([
        'count' => $post->count + 1
    ]);


    return View::make('posts', compact('post'));
}

public function show($id) 
{
   $post = Post::find($id);
   $post->update([
    'count' => $post->count + 1
   ]);
   return View::make('posts', compact('post'));
}

You have two requirements:

  1. To increase view count for every view.
public function show($id) 
{
    $post = Post::find($id);

    Post::update([
        'count' => $post->count + 1
    ]);


    return View::make('posts', compact('post'));
}
  1. To display the most viewed blogs according to the view count
$post = Post::orderBy('count', 'DESC')->first();
  1. Additionaly, To display the most viewed blogs list according to the view count,
$post = Post::orderBy('count', 'DESC')->get();