PHP code example of zing / laravel-view

1. Go to this page and download the library: Download zing/laravel-view library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zing / laravel-view example snippets


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

class User extends Model
{
    use Visitor;
}

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

class Subject extends Model
{
    use Visitable;
}

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();

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();

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);
    }
}
bash
php artisan vendor:publish --tag=visit-config
php artisan vendor:publish --tag=visit-migrations
bash
php artisan migrate