Giter Site home page Giter Site logo

wesley-sinde / how-to-save-post-view-count-in-laravel Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 2 KB

Today i will show you how to Implement Post views count using Laravel . i will add this using easy and simple method for small Projects click counter or post visit count . We will create this traffic counter using Laravel

how-to-save-post-view-count-in-laravel's Introduction

How to save post view count in laravel?

✍ Laravel

⚇ by wesley

⌚ 01-02-20222

Today i will show you how to Implement Post views count using Laravel . i will add this using easy and simple method for small Projects click counter or post visit count . We will create this traffic counter using Laravel.

show all posts

<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAllPost()
    {
        $posts = Post::all();
        return view('posts.all-posts', compact('posts'));
    }


all-posts.blade.php

@foreach ($posts as $post)
    <h3>{{ $post->name }}</h6>
    <a href="{{ route('posts.show',$post->id) }}" 
      class="btn btn-info">View
    </a>
@endforeach

Write Post Route

<?php

use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;

Route::get('posts',[ PostController::class,'getAllPost']);

Now write Logic View Count

we can implement two way by using(++) or increment() method

1) using(++)

public function getViewPost($id)
    {
        $post = Post::find($id);
        $post->view_count++;
        $post->save();
        return view('posts.view-post', compact('post'));
    }

2) increment() recommended

public function getViewPost($id)
    {
        $post = Post::find($id);
         $post->increment('view_count');
        $post->save();
        return view('posts.view-post', compact('post'));
    }

i personally use increment() because it more readable and easy to understand

 <?php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
Route::get('posts/{id}',[ PostController::class,'getViewPost']);

how-to-save-post-view-count-in-laravel's People

Contributors

wesley-sinde avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.