PHP code example of timgavin / laravel-block

1. Go to this page and download the library: Download timgavin/laravel-block 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/ */

    

timgavin / laravel-block example snippets


namespace App\Models;

use TimGavin\LaravelBlock\LaravelBlock;

class User extends Authenticatable
{
    use LaravelBlock;
}

return [
    'cache_duration' => 60 * 60 * 24, // 24 hours in seconds
    'dispatch_events' => true,
    'user_model' => null, // falls back to auth config
];

auth()->user()->block($user);

auth()->user()->unblock($user);

auth()->user()->toggleBlock($user);

@if (auth()->user()->isBlocking($user))
    You are blocking this user.
@endif

@if (auth()->user()->isBlockedBy($user))
    This user is blocking you.
@endif

@if (auth()->user()->isMutuallyBlocking($user))
    You are both blocking each other.
@endif

@if (auth()->user()->hasAnyBlockWith($user))
    There is a block relationship.
@endif

auth()->user()->getBlockingCount();

auth()->user()->getBlockersCount();

auth()->user()->getBlocking();

auth()->user()->getBlockingPaginated(15);

auth()->user()->getBlockers();

auth()->user()->getBlockersPaginated(15);

// default limit is 5
auth()->user()->getLatestBlockers($limit);

auth()->user()->getBlockingIds();

auth()->user()->getBlockersIds();

auth()->user()->getBlockingAndBlockersIds();

auth()->user()->getAllBlockUserIds();

$userIds = $users->pluck('id')->toArray();
$statuses = auth()->user()->getBlockStatusForUsers($userIds);

// Returns: [userId => ['is_blocking' => bool, 'is_blocked_by' => bool]]

$status = auth()->user()->getBlockStatusFor($user);

// Returns: ['is_blocking' => bool, 'is_blocked_by' => bool]

// Exclude users blocked by or blocking the authenticated user
User::query()->excludeBlocked()->get();

// Exclude users blocked by or blocking a specific user
User::query()->excludeBlocked($user)->get();

$user->blocks;

$user->blockers;

$user->getBlockingRelationship($otherUser);

$user->getBlockerRelationship($otherUser);

$user->getBlockRelationshipsWith($otherUser);

auth()->user()->cacheBlocking();

// custom duration in seconds
auth()->user()->cacheBlocking(3600);

auth()->user()->getBlockingCache();

auth()->user()->cacheBlockers();

auth()->user()->getBlockersCache();

auth()->user()->clearBlockingCache();

auth()->user()->clearBlockersCache();

auth()->user()->clearBlockersCacheFor($user);

auth()->user()->clearBlockingCacheFor($user);

use TimGavin\LaravelBlock\Events\UserBlocked;
use TimGavin\LaravelBlock\Events\UserUnblocked;

Event::listen(UserBlocked::class, function ($event) {
    // $event->userId - the user who blocked
    // $event->blockedId - the user who was blocked
});

Event::listen(UserUnblocked::class, function ($event) {
    // $event->userId - the user who unblocked
    // $event->unblockedId - the user who was unblocked
});

'dispatch_events' => false,

use TimGavin\LaravelBlock\Models\Block;

// Get blocks where a user is blocking others
Block::whereUserBlocks($userId)->get();

// Get blocks where a user is being blocked
Block::whereUserIsBlockedBy($userId)->get();

// Get all blocks involving a user
Block::involvingUser($userId)->get();

php artisan migrate
bash
php artisan vendor:publish --tag=laravel-block-config