PHP code example of zaichaopan / online-status

1. Go to this page and download the library: Download zaichaopan/online-status 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/ */

    

zaichaopan / online-status example snippets


//...
use Zaichaopan\OnlineStatus\HasOnlineStatus;

class User extends Model
{
    use HasOnlineStatus;

}

// ...
class User extends Model
{
    use HasOnlineStatus;

    public static function getOnlineExpirationInMinutes(): int
    {
        return 10;
    }
}

// App\Http\Kernel.php
class Kernel extends HttpKernel
{
    // ...
    protected $middlewareGroups = [
        'web' => [
           \\...
           \Zaichaopan\OnlineStatus\Middleware\UserOnline::class
        ],
        //...
    ];
}

//
class UserOnline
{
   // ...
    public function handle(Request $request, Closure $next)
    {
        optional($request->user())->online();

        return $next($request);
    }
}

$status = $user->isOnline;

// or
$status = $user->isOnline();

$onlineUserCount = User::onlineCount();

$onlineUsers = User::ofOnline()->get();

// or
$onlineUsers = User::ofOnline()->paginate();

$user->online();

$use->offline();

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'Zaichaopan\OnlineStatus\Events\Online' => [
        'App\Listeners\Online',
    ],

    'Zaichaopan\OnlineStatus\Events\Offline' => [
        'App\Listeners\Offline',
    ]
];