zing/laravel-view

This package is abandoned and no longer maintained. The author suggests using the zing/laravel-eloquent-view package instead.

User visit behaviour for Laravel.

3.3.0 2024-03-18 14:48 UTC

README

User visit behaviour for Laravel.

Latest Stable Version Total Downloads Latest Unstable Version License

Introduction

It used to record the number of visits to the model(documentation/subject/question).

Installation

Requirements

Instructions

Require Laravel Visit using Composer.

composer require laravel-interaction/visit

Publish configuration and migrations

php artisan vendor:publish --tag=visit-config
php artisan vendor:publish --tag=visit-migrations

Run database migrations.

php artisan migrate

Usage

Setup Visitor

use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Visit\Concerns\Visitor;

class User extends Model
{
    use Visitor;
}

Setup Visitable

use Illuminate\Database\Eloquent\Model;
use LaravelInteraction\Visit\Concerns\Visitable;

class Subject extends Model
{
    use Visitable;
}

Visitor

use LaravelInteraction\Visit\Tests\Models\Subject;
/** @var \LaravelInteraction\Visit\Tests\Models\User $user */
/** @var \LaravelInteraction\Visit\Tests\Models\Subject $subject */
// Visit to Visitable
$user->visit($subject);

// Compare Visitable
$user->hasVisited($subject);
$user->hasNot
Visited($subject);

// Get visited info
$user->visitVisitors()->count(); 

// with type
$user->visitVisitors()->withType(Subject::class)->count(); 

// get visited subjects
Subject::query()->whereVisitedBy($user)->get();

// get subjects doesnt visited
Subject::query()->whereNotVisitedBy($user)->get();

Visitable

use LaravelInteraction\Visit\Tests\Models\User;
use LaravelInteraction\Visit\Tests\Models\Subject;
/** @var \LaravelInteraction\Visit\Tests\Models\User $user */
/** @var \LaravelInteraction\Visit\Tests\Models\Subject $subject */
// Compare Visitor
$subject->isVisitedBy($user); 
$subject->isNotVisitedBy($user);
// Get visitors info
$subject->visitors->each(function (User $user){
    echo $user->getKey();
});

$subjects = Subject::query()->withVisitorsCount()->get();
$subjects->each(function (Subject $subject){
    // like uv
    echo $subject->visitors()->count(); // 1100
    echo $subject->visitors_count; // "1100"
    echo $subject->visitorsCount(); // 1100
    echo $subject->visitorsCountForHumans(); // "1.1K"
    // like pv
    echo $subject->visitableVisits()->count(); // 1100
    echo $subject->visits_count; // "1100"
    echo $subject->visitsCount(); // 1100
    echo $subject->visitsCountForHumans(); // "1.1K"
});
$subjects = Subject::query()->withVisitorsCount(function ($query){
    return $query->whereKey(1);
})->get();

With Api Request

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use LaravelInteraction\Visit\Tests\Models\Subject;
use Illuminate\Http\Resources\Json\JsonResource;

class AuctionController extends Controller
{
    public function show($id, Request $request){
        $subject = Subject::query()->findOrFail($id);
        dispatch(function () use ($subject, $request) {
            $subject->record($request);
        })->afterResponse();
        return new JsonResource($subject);
    }
}

Events

License

Laravel Eloquent Visit is an open-sourced software licensed under the MIT license.