timgavin/laravel-block

A User can block another User

1.1.10 2024-04-27 15:34 UTC

This package is auto-updated.

Last update: 2024-10-27 16:42:58 UTC


README

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 tim@timgavin.name instead of using the issue tracker.

License

MIT. Please see the license file for more information.