Easily exclude model entities from eloquent queries

Overview

Social Card of Laravel Excludable

Laravel Excludable

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Easily exclude model entities from eloquent queries.

This package allows you to define a subset of model entities who should be excluded from eloquent queries. You will be able to override the default Exclusion model and its associated migration, so you can eventually restrict the exclusion context by defining the entity that should effectively exclude the subset.

An example usage could be an application with a multi tenant scenario and a set of global entities. While those entities should be accessible by all tenants, some of them might want to hide a subset of those entities for their users. You can find an example in the Usage section.

Installation

You can install the package via composer:

composer require h-farm/laravel-excludable

You can publish and run the migrations with:

php artisan vendor:publish --provider="HFarm\Excludable\ExcludableServiceProvider" --tag="excludable-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="HFarm\Excludable\ExcludableServiceProvider" --tag="excludable-config"

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Exclusion model
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the exclusion model.
    |
    */

    'exclusion_model' => HFarm\Excludable\Models\Exclusion::class,
];

Usage

Basic

To use the package, add the HFarm\Excludable\Excludable trait to all models you want to make excludable.

Here's an example model including the Excludable trait:

<?php

namespace App\Models;

use HFarm\Excludable\Excludable;

class Article extends Model
{
    use Excludable;

    protected $fillable = [
        'title',
        'body',
    ];
}

Now you can just query for a specific article entity and mark it as excluded.

use App\Models\Article;

$article = Article::query()->findOrFail(1)

$article->addToExclusion();

$article->excluded(); // returns true

That's all!

The package will add the given entity to the exclusions table, so all article related queries will exclude it.

use App\Models\Article;

Article::findOrFail(1); // throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException

Include excluded entities

use App\Models\Article;

Article::withExcluded()->get(); // queries all models, including those marked as excluded 

Only show excluded entities

use App\Models\Article;

Article::onlyExcluded()->get(); // queries only excluded entities

Event handling

The package automatically throws two separate events when excluding an entity:

  • excluding which is thrown before the entity is actually excluded. This could be useful, for example, with an observer which listens to this event and does some sort of 'validation' to the related entity. If the given validation does not succeed, you can just return false, and the entity will not be excluded;
  • excluded which is thrown right after the entity has been marked as excluded.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
Releases(3.0.0)
Owner
H-FARM
H-FARM
Tiny php mysql lib (PDO-based) with handy fetch/update functionality, supports both SQL and parametric queries

Micro PHP mysql lib (~ 200 lines of code) with ultra powerful CRUD for faster than ever development: parametric fetch/insert/update/delete (based on a

Mr Crypster 18 Dec 10, 2022
A simple library for managing database connections, results pagination and building queries in PHP

PHP lions-software-database-manager This is a simple library for managing database connections, results pagination and building queries in PHP. Esta é

Lions Software 0 Feb 7, 2022
A data mapper implementation for your persistence model in PHP.

Atlas.Orm Atlas is a data mapper implementation for persistence models (not domain models). As such, Atlas uses the term "record" to indicate that its

null 427 Dec 30, 2022
PHP Object Model Manager for Postgresql

POMM: The PHP Object Model Manager for Postgresql Note This is the 1,x version of Pomm. This package is not maintained anymore, the stable Pomm 2.0 is

Grégoire HUBERT 161 Oct 17, 2022
Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.

Baum Baum is an implementation of the Nested Set pattern for Laravel 5's Eloquent ORM. For Laravel 4.2.x compatibility, check the 1.0.x branch branch

Estanislau Trepat 2.2k Jan 3, 2023
⚡️ Models like Eloquent for Elasticsearch.

Elasticsearch Eloquent 2.x This package allows you to interact with Elasticsearch as you interact with Eloquent models in Laravel. Requirements PHP >=

Sergey Sorokin 111 Dec 19, 2022
Extensions for the Eloquent ORM

Sofa/Eloquence Easy and flexible extensions for the Eloquent ORM. Currently available extensions: Searchable query - crazy-simple fulltext search thro

Jarek Tkaczyk 1.1k Dec 20, 2022
Simple Enum cast for Eloquent ORM using myclabs/php-enum.

Enum cast for Eloquent Simple Enum cast for Eloquent ORM using myclabs/php-enum. Requirements PHP 7.3 or higher Laravel 8.0 or higher Installation You

Orkhan Ahmadov 5 Apr 21, 2022
Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.

Eloquent Filter Eloquent Filter adds custom filters to your Eloquent Models in Laravel. It's easy to use and fully dynamic. Table of Content Introduct

Mehdi Fathi 327 Dec 28, 2022
Eloquent Repository implementation

Eloquent Repository Eloquent Repository using nilportugues/repository as foundation. Installation Use Composer to install the package: $ composer requ

Nil Portugués Calderó 17 Feb 12, 2022
Eloquent MongoDB Repository Implementation

Eloquent MongoDB Repository Eloquent MongoDB Repository using nilportugues/repository as foundation, using jenssegers/mongodb. Installation Use Compos

Nil Portugués Calderó 18 Feb 12, 2022
Easily anonymize sensitive data through eloquent queries

Laravel Encryptable This package allows you to anonymize sensitive data (like the name, surname and email address of a user) similarly to Laravel's En

H-FARM Innovation 93 Sep 6, 2022
Easily build Eloquent queries from API requests

Build Eloquent queries from API requests This package allows you to filter, sort and include eloquent relations based on a request. The QueryBuilder u

Spatie 3.5k Jan 7, 2023
This is wegare tools but all-in-one installer only, exclude GUI

All In One Installer for Wegare Tools This is wegare tools, but this is all-in-one. Here is the source https://github.com/wegare123?tab=repositories I

Helmi Amirudin 3 Jul 29, 2022
Shell script for Git module deployment with include/exclude filters.

Deploy multiple Git repositories in an unique folder modgit is a shell script for deploying multiple Git repositories in root folder of any project, w

Johann Reinké 175 Nov 22, 2022
Quickly and easily expose Doctrine entities as REST resource endpoints with the use of simple configuration with annotations, yaml, json or a PHP array.

Drest Dress up doctrine entities and expose them as REST resources This library allows you to quickly annotate your doctrine entities into restful res

Lee Davis 88 Nov 5, 2022
Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Laravel comments - This package enables to easily associate comments to any Eloquent model in your Laravel application

Rubik 4 May 12, 2022
Easily implement optimistic Eloquent model locking feature to your Laravel app.

quarks/laravel-locking Easily implement optimistic Eloquent model locking feature to your Laravel app. Installation composer require quarks/laravel-lo

Quarks 2 Jun 21, 2022
LaraCache is an ORM based package for Laravel to create, update and manage cache items based on model queries

LaraCache Using this package, you can cache your heavy and most used queries. All you have to do is to define the CacheEntity objects in the model and

Mostafa Zeinivand 202 Dec 19, 2022
The missing laravel helper that allows you to inspect your eloquent queries with it's bind parameters

Laravel Query Inspector The missing laravel helper that allows you to ispect your eloquent queries with it's bind parameters Motivations Let's say you

Mouad ZIANI 59 Sep 25, 2022