Skip to content

timgavin/laravel-block

Repository files navigation

Laravel Block

Latest Version on Packagist Total Downloads Build Status StyleCI

A simple Laravel package for blocking users.

Requirements

  • Laravel 9 or greater.
  • Laravel User model.

Installation

Via Composer

$ composer require timgavin/laravel-block

Import Laravel Block into your User model and add the trait.

namespace App\Models;

use TimGavin\LaravelBlock\LaravelBlock;

class User extends Authenticatable
{
    use LaravelBlock;
}

Then run migrations.

php artisan migrate

Usage

Block a user.

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

Unblock a user.

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

Check if a user is blocking another user.

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

Check if a user is blocked by another user.

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

Returns the users a user is blocking.

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

Returns the users who are blocking a user.

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

Returns an array of IDs of the users a user is blocking.

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

Returns an array of IDs of the users who are blocking a user.

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

Returns an array of IDs of the users a user is blocking, and who is blocking a user

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

Caches the IDs of the users a user is blocking. Default is 1 day.

// 1 day
auth()->user()->cacheBlocking();

// 1 hour
auth()->user()->cacheBlocking(3600);

// 1 month
auth()->user()->cacheBlocking(Carbon::addMonth());

Returns an array of IDs of the users a user is blocking.

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

Caches the IDs of the users who are blocking a user. Default is 1 day.

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

Returns an array of IDs of the users who are blocking a user.

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

Clears the Blocking cache

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

Clears the Blockers cache

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

Change log

Please see the changelog for more information on what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

MIT. Please see the license file for more information.