jm-inc / laravel-viewable
A simple Laravel 8 package to count views for models.
0.1
2021-07-16 10:37 UTC
Requires
- hisorange/browser-detect: ^4.4
Requires (Dev)
- orchestra/testbench: ^6.19
README
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
- Add this trait
JM\Viewable\InteractsWithViews
to the model you want to count views for. - In your
show
controller method, use$model->viewed()
to count a view. - 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(); // ... } }