PHPackages                             imanghafoori/laravel-temp-tag - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. imanghafoori/laravel-temp-tag

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

imanghafoori/laravel-temp-tag
=============================

Laravel Temporary Tag simplify tagging Eloquent models.

v1.2.18(1y ago)1018.6k11[1 issues](https://github.com/imanghafoori1/laravel-temp-tag/issues)MITPHPPHP ^7.1.3|8.\*CI passing

Since Sep 1Pushed 1y ago3 watchersCompare

[ Source](https://github.com/imanghafoori1/laravel-temp-tag)[ Packagist](https://packagist.org/packages/imanghafoori/laravel-temp-tag)[ RSS](/packages/imanghafoori-laravel-temp-tag/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (9)Versions (36)Used By (0)

Eloquent Temporary Tags
=======================

[](#eloquent-temporary-tags)

### Auto-expiring tags with additional payload data on any eloquent model.

[](#auto-expiring-tags-with-additional-payload-data-on-any-eloquent-model)

[![image](https://user-images.githubusercontent.com/6961695/93660285-6a935180-fa62-11ea-98ca-5a7675c6bd6a.png)](https://user-images.githubusercontent.com/6961695/93660285-6a935180-fa62-11ea-98ca-5a7675c6bd6a.png)

#### Built with ❤️ for every smart Laravel developer

[](#built-with-heart-for-every-smart-laravel-developer)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3cd323fc7efebcbefdafb74b059e8137dc9b04aa3a67bc4de9c0433b4dc9c7b4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696d616e676861666f6f7269312f6c61726176656c2d74656d702d7461672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/imanghafoori1/laravel-temp-tag/?branch=master)[![StyleCI](https://camo.githubusercontent.com/75254730c3e9fde80febafe85231e7f354e9f350afe6f804652481e7121d7d5c/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3136363633313634332f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/166631643?branch=master)[![Software License](https://camo.githubusercontent.com/7b9d0faf11330a88ea80a58ccf5491f699d5edf7ddac76ad40e2a9dc6a77fd76/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d726f756e642d737175617265)](LICENSE.md)[![Check Imports](https://github.com/imanghafoori1/laravel-temp-tag/actions/workflows/check_import.yml/badge.svg?branch=master)](https://github.com/imanghafoori1/laravel-temp-tag/actions/workflows/check_import.yml)

### Installation:

[](#installation)

```
composer require imanghafoori/laravel-temp-tag
```

Then you can publish and migrate to create the needed tags table

```
php artisan vendor:publish
php artisan migrate
```

### Sample Application in laravel 8:

[](#sample-application-in-laravel-8)

In this Daily Task app, you can mark your tasks as `complete`,`failed`,`ignored` and etc but they rollback back to the default state at the end of the day, so you can re-mark them for tomorrow.

### Use cases:

[](#use-cases)

- You want to ban a user, but only for a week.
- You want to give someone VIP access, but only for a month.
- Comments approval by admin.
- You want to put a product in a slider until the weekend.
- Storing each user preferences, can be done by attaching a 'settings' tag and the preferences as payload.
- Storing users likes and dislikes on any model.
- Scheduling models to be published in the future.
- Coupon code for specific products on specific periods.

Then you put a temporary tag on them and check to see if the model has the tag.

### Tag Payload:

[](#tag-payload)

You can also store some additional json data, for example why the user got banned, or who banned the user, or a slug or a translation of the title.

This is done by passing the third argument as an array to the `->tagIt(...)` method

### Keynotes:

[](#keynotes)

- Tags can also be permanent.
- We do not touch your existing tables in migrations.
- You can put tag on any eloquent model or any other object with `getKey` and `getTable` methods on it.

---

### Example:

[](#example)

1- Tag a user until tomorrow

```
  $user = User::find(1);
  $tomorrow = Carbon::now()->addDay();
  $note = ['reason' => 'You were nasty!']; // You can optionally store additional data in a json column.

  tempTags($user)->tagIt('banned', $tomorrow, $note);

  tempTags($user)->tagIt('banned');  // will never expire
```

2- After an hour the tag is still active, so:

```
  $user = User::find(1);

  $tagObj = tempTags($user)->getActiveTag('banned');  // isActive();         // true
    $tagObj->isPermanent();      // false
    $tagObj->title === 'banned'; // true
    $tagObj->payload;            // ['reason' => 'You were nasty!']
    $tagObj->expiresAt();        // Carbon instance
  }
```

3- After a week, the tag gets expired so:

```
  $user = User::find(1);
  $tagObj = tempTags($user)->getTag('banned');  // isActive();         // false
    $tagObj->isPermanent();      // false
    $tagObj->title === 'banned'; // true
    $tagObj->expiresAt();        // Carbon instance
  }
```

---

Getting payload data:

There are 2 ways to get payload data :

You can use `getPayload` method

```
  $tagObj->getPayload('reason');       //  'You were nasty!'
  $tagObj->getPayload();               //  ['reason' => 'You were nasty!']
  $tagObj->getPayload('missing_key');  //  null
```

or You can act like eloquent attributes :

```
  $tagObj->reason;       //  'You were nasty!'
  $tagObj->payload;      //  ['reason' => 'You were nasty!']
  $tagObj->missing_key;  //  null
```

---

#### Deleting tags:

[](#deleting-tags)

```
  $user = User::find(1);

  tempTags($user)->unTag('banned');           // single string

  tempTags($user)->unTag('bann*');            // using a wildcard

  tempTags($user)->unTag(['banned', 'man']);  // an array of tags to delete

  tempTags($user)->deleteExpiredTags();       // all the expired tags, bye bye.
```

**Note:** You can also use \* wildcard which matcher 0 or more characters (just like % in sql)

**Note:** These fire "deleting" and "deleted" eloquent events for each one of them.

Expire the tag with title of "banned" right now:

```
 tempTags($user)->expireNow('banned');  // updates the value of expire_at to now() and deletes from cache
```

---

These methods just do what they say:

```
  $actives = tempTags($user)->getAllActiveTags();  // A collect of "TempTag" model objects.

  $expired = tempTags($user)->getAllExpiredTags();

  $all = tempTags($user)->getAllTags();
```

---

### Fetch only tagged models:

[](#fetch-only-tagged-models)

Let's say you have a slider for your `Product` model and you want to show only those records which are tagged with 'slider'.

First you have to put `Imanghafoori\Tags\Traits\hasTempTags` trait on the `Product` model.

```
class Product extends Model
{
  use hasTempTags;

  ...
}
```

Now you can perform these queries:

```
Product::hasActiveTags('slider')->where(...)->get();

// Only if the tag of model is expired and it has the specified title.
Product::hasExpiredTags('slider')->where(...)->get();

// To fetch regardless of expiration date of tags, only the title matters.
Product::hasTags('slider')->where(...)->get();
```

**Note:** If you pass an array of tags it acts like a `whereIn()`, so if the row has one of tags if will be selected.

If you want to find products which will be active until tomorrow, you can use `hasActiveTagsAt`, or `hasNotActiveTagsAt` as illustrated below:

```
Product::hasActiveTagsAt('slider', now()->addDay())->where(...)->get();

Product::hasNotActiveTagsAt('slider', now()->addDay())->where(...)->get();
```

#### Example:

[](#example-1)

When you want to send a notification to users, just 24 hours before their VIP account gets finished, so that they can charge.

```
User::hasActiveTag('VIP')
    ->hasNotActiveTagsAt('VIP', now()->addDay())
    ->get();
```

---

### Absence of tags:

[](#absence-of-tags)

```
Product::hasNotActiveTags('slider')->where(...)->get();

Product::hasNotExpiredTags('slider')->where(...)->get();

Product::hasNotTags('slider')->where(...)->get();
```

### Example:

[](#example-2)

When you want your article to be published in the future, go can tag your articles with a custom tag title like: "hidden" And fetch them like this:

```
$articles = Article::hasNotActiveTags('hidden')->where(...)->get();
```

So, as long as the 'hidden' tag is active the article does not show on the list and when the tag gets expired the article goes live.

---

### Filtering based on payload:

[](#filtering-based-on-payload)

```
$product1 = Product::find(1);
$product2 = Product::find(1);

tempTags($product1)->tagIt('status', $tomorrow, ['value' => 'sold_out']);
tempTags($product2)->tagIt('status', $tomorrow, ['value' => 'sold_out']);

// now we have tagged these two rows we can fetch them later on with the below query

...

Product::hasActiveTags('status', ['value' => 'sold_out'])->where(...)->get();
```

The above example gives you the products with active status tag which have also payload data with the specified key and value.

**Note:** You can also use \* wildcard which matcher 0 or more characters (just like % in sql):

```
Product::hasActiveTags('stat*', ['value' => 'sold_out'])->where(...)->get();
```

---

### Advanced Usage:

[](#advanced-usage)

Each and every `has...Tag` method also has a twin `orHas...Tag` so you can put multiple conditions in the query.

Remember that they both should reside in a `->where(function ($q) {` beside one another.

```
Post::where('user_id', 12)->where(function ($q) {
    $q->hasActiveTags('status', ['value' => 'active'])
    ->orHasActiveTags('status', ['value' => 'promoted']);
})->get();
```

---

### Auto-delete Expired tags:

[](#auto-delete-expired-tags)

In order to save disk space and have faster db queries you may want to delete the expired tags.

After you have performed the basic installation you can start using the tag:delete-expired command. In most cases you'll want to schedule this command so you don't have to manually run it everytime you need to delete expired tags.

```
// app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
    // Will run:  php artisan  tag:delete-expired
    $schedule->command( 'tag:delete-expired' )->everyDay();
}
```

---

### 🙋 Contributing:

[](#raising_hand-contributing)

If you find an issue or have a better way to do something, feel free to open an issue, or a pull request.

### ⭐ Your Stars Make Us Do More ⭐

[](#star-your-stars-make-us-do-more-star)

As always if you found this package useful, and you want to encourage us to maintain and work on it, just press the star button to declare your willingness.

---

More from the author:
---------------------

[](#more-from-the-author)

### Laravel Microscope

[](#laravel-microscope)

💎 It automatically find bugs in your laravel app

-

### Laravel HeyMan

[](#laravel-heyman)

💎 It allows you to write expressive code to authorize, validate, and authenticate.

-

**This package is originally inspired by the cybercog/laravel-ban package.**

-

```
It's not that I'm so smart, it's just that I stay with problems longer.

"Albert Einstein"

```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance44

Moderate activity, may be stable

Popularity36

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 91.8% 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

Every ~54 days

Recently: every ~278 days

Total

31

Last Release

443d ago

Major Versions

v0.0.5 → v1.0.02020-09-17

PHP version history (2 changes)v0.0.1PHP ^7.1.3

v1.2.11PHP ^7.1.3|8.\*

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6961695?v=4)[Iman](/maintainers/imanghafoori1)[@imanghafoori1](https://github.com/imanghafoori1)

---

Top Contributors

[![imanghafoori1](https://avatars.githubusercontent.com/u/6961695?v=4)](https://github.com/imanghafoori1 "imanghafoori1 (123 commits)")[![rahi69](https://avatars.githubusercontent.com/u/35202922?v=4)](https://github.com/rahi69 "rahi69 (4 commits)")[![reziamini](https://avatars.githubusercontent.com/u/29504334?v=4)](https://github.com/reziamini "reziamini (2 commits)")[![mohsenfathipour](https://avatars.githubusercontent.com/u/32642574?v=4)](https://github.com/mohsenfathipour "mohsenfathipour (1 commits)")[![amirsadeghi1](https://avatars.githubusercontent.com/u/26359326?v=4)](https://github.com/amirsadeghi1 "amirsadeghi1 (1 commits)")[![sa33drs](https://avatars.githubusercontent.com/u/52917860?v=4)](https://github.com/sa33drs "sa33drs (1 commits)")[![FX-Max](https://avatars.githubusercontent.com/u/5110882?v=4)](https://github.com/FX-Max "FX-Max (1 commits)")[![mehradsadeghi](https://avatars.githubusercontent.com/u/31504728?v=4)](https://github.com/mehradsadeghi "mehradsadeghi (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/imanghafoori-laravel-temp-tag/health.svg)

```
[![Health](https://phpackages.com/badges/imanghafoori-laravel-temp-tag/health.svg)](https://phpackages.com/packages/imanghafoori-laravel-temp-tag)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[kirschbaum-development/commentions

A package to allow you to create comments, tag users and more

12369.2k](/packages/kirschbaum-development-commentions)

PHPackages © 2026

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