LaravelPackages.net
Acme Inc.
Toggle sidebar
jm-inc/laravel-viewable

A simple Laravel 8 package to count views for models.

26
1
0.1
About jm-inc/laravel-viewable

jm-inc/laravel-viewable is a Laravel package for a simple laravel 8 package to count views for models.. It currently has 1 GitHub stars and 26 downloads on Packagist (latest version 0.1). Install it with composer require jm-inc/laravel-viewable. Discover more Laravel packages by jm-inc or browse all Laravel packages to compare alternatives.

Last updated

Laravel Viewable

Latest Stable Version GitHub license GitHub Tests Action Status Total Downloads

A simple Laravel 8 package to count views for models.

Installation

composer require jm-inc/laravel-viewable
php artisan migrate
php artisan vendor:publish --tag=viewable-config # publish the configuration (optional)

Setup

  1. Add this trait JM\Viewable\InteractsWithViews to the model you want to count views for.
  2. In your show controller method, use $model->viewed() to count a view.
  3. If the primary key of your model is not id set $viewable_id = 'primary-key in your model.

Example

Your model should look like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use JM\Viewable\InteractsWithViews;

class Post extends Model
{
    use HasFactory, InteractsWithViews;
}

Your show route method should look like this:

<?php

namespace App\Http\Controllers;

use App\Models\Post;

class PostController extends Controller
{
    public function show(Post $post)
    {
        // ...
        $post->viewed();
        // ...
    }
}
Comments