PHPackages                             andy-shi88/laravel-eloquent-flags - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. andy-shi88/laravel-eloquent-flags

ActiveLibrary[Database &amp; ORM](/categories/database)

andy-shi88/laravel-eloquent-flags
=================================

working with flags field on laravel eloquent integer field

0.0.1(1y ago)04MITPHPPHP &gt;=8.0CI passing

Since Mar 22Pushed 1y ago1 watchersCompare

[ Source](https://github.com/andy-shi88/laravel-eloquent-flags)[ Packagist](https://packagist.org/packages/andy-shi88/laravel-eloquent-flags)[ RSS](/packages/andy-shi88-laravel-eloquent-flags/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

A trait to make Eloquent models handle flags in `integer` column
================================================================

[](#a-trait-to-make-eloquent-models-handle-flags-in-integer-column)

[![test](https://github.com/andy-shi88/laravel-eloquent-flags/actions/workflows/test.yml/badge.svg)](https://github.com/andy-shi88/laravel-eloquent-flags/actions/workflows/test.yml/badge.svg)

This package contains a trait HasFlags to make Eloquent models able to handle multiple flag values using just integer column type. The column type can be any unsigned number type.

### Install

[](#install)

```
composer require andy-shi88/laravel-eloquent-flags

```

### Limitation

[](#limitation)

The number of flags per column this can handle depends on its datatype.

- unsignedInteger(): 32 flags
- unsignedBigInteger(): 64 flags

### Usages

[](#usages)

- ### [Setup Models](#setup-models)

    [](#setup-models)
- ### [Querying](#querying)

    [](#querying)

###

[](#)

### Setup Models

[](#setup-models-1)

- With the schema like this.

```
Schema::create('person', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->unsignedInteger('category')->nullable(); // can handle nullable field
    $table->unsignedInteger('ownership')->default(0);
    $table->timestamps();
});
```

- Implement `Flagable` interface, use `HasFlags` trait, and set `$flagableColumns` to column that want to be used as integer flags.
    - `$flagableColumn` is formatted as: ```
        [
            'column_1': [...$labels],
            'column_2': [...$labels],
        ]
        ```

```
use AndyShi88\LaravelEloquentFlags\HasFlags;
use AndyShi88\LaravelEloquentFlags\Interfaces\Flagable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Person extends Model implements Flagable
{
    use HasFactory;
    use HasFlags;

    public $table = 'person';

    public $flagableColumns = [
        'ownership' => ['car', 'bike', 'house', 'computer', 'phone', 'console'],
        'category' => ['asian', 'white', 'black', 'hispanic', 'middle-eastern'],
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var list
     */
    protected $fillable = [
        'name',
        'ownership',
        'category',
    ];
}
```

###

[](#-1)

### Query Usages

[](#query-usages)

- Store data, this will save the `ownership` and `category` field as `integer` in the database, but readable as array of labels we specified.

```
$a = Person::factory()->create([
    'name' => 'a',
    'ownership' => ['car', 'house', 'phone'],
    'category' => ['asian', 'white'],
]);
$b = Person::factory()->create([
    'name' => 'b',
    'ownership' => ['car', 'bike', 'house', 'computer', 'phone', 'console'],
    'category' => ['white', 'hispanic'],
]);
$c =  Person::factory()->create([
    'name' => 'c',
    'ownership' => ['car', 'bike'],
    'category' => ['asian', 'middle-eastern'],
]);
$d = Person::factory()->create([
    'name' => 'd',
    'ownership' => ['computer', 'phone', 'console'],
    'category' => ['asian', 'middle-eastern'],
]);
```

- ### `whereSome()`, return data where the column have all of the values specified

    [](#wheresome-return-data-where-the-column-have-all-of-the-values-specified)

```
$resSome = Person::whereSome([
    'column' => 'ownership',
    'values' => ['car', 'bike']
])->get(); // return $b, $c
```

- ### `whereAll()`, return data where the column have exactly all of the values specified

    [](#whereall-return-data-where-the-column-have-exactly-all-of-the-values-specified)

```
$resAll = Person::whereAll([
    'column' => 'ownership',
    'values' => ['car', 'house', 'phone']
])->get(); // return $a
```

- ### `whereIntersect()`, return data where the column have at least one of the specified value

    [](#whereintersect-return-data-where-the-column-have-at-least-one-of-the-specified-value)

```
$resIntersect = Person::whereIntersect([
    'column' => 'ownership',
    'values' => ['car', 'house', 'phone']
])->get(); // return $a, $b, $c, $d

// specifying number of minimal intersected value [mysql]
$resIntersect = Person::whereIntersect([
    'column' => 'ownership',
    'values' => ['car', 'house', 'phone']
], 2)->get(); // return $a, $b
```

We'll also by default get the readable array of the fields:

```
$a = Person::factory()->create([
    'name' => 'a',
    'ownership' => ['car', 'house', 'phone'],
    'category' => ['asian', 'white'],
]);
dd([
    'ownership' => $a->ownership,
    'category' => $a->category
]);

// we'll see
/**
 * array:2 [
 * "ownership" => array:3 [
 *   0 => "car"
 *   1 => "house"
 *  2 => "phone"
 * ]
 * "category" => array:2 [
 *   0 => "asian"
 *   1 => "white"
 * ]
*]
*/
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance46

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity32

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

417d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7770e7ba57dcf3daa66f101a3e67c2f46a698ab8a20b613785a78c5aa7d3340d?d=identicon)[andy-shi88](/maintainers/andy-shi88)

---

Top Contributors

[![andy-shi88](https://avatars.githubusercontent.com/u/19222818?v=4)](https://github.com/andy-shi88 "andy-shi88 (17 commits)")

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/andy-shi88-laravel-eloquent-flags/health.svg)

```
[![Health](https://phpackages.com/badges/andy-shi88-laravel-eloquent-flags/health.svg)](https://phpackages.com/packages/andy-shi88-laravel-eloquent-flags)
```

###  Alternatives

[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[aglipanci/laravel-eloquent-case

Adds CASE statement support to Laravel Query Builder.

115157.2k](/packages/aglipanci-laravel-eloquent-case)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
