Download the PHP package imanghafoori/laravel-temp-tag without Composer
On this page you can find all versions of the php package imanghafoori/laravel-temp-tag. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download imanghafoori/laravel-temp-tag
More information about imanghafoori/laravel-temp-tag
Files in imanghafoori/laravel-temp-tag
Package laravel-temp-tag
Short Description Laravel Temporary Tag simplify tagging Eloquent models.
License MIT
Informations about the package laravel-temp-tag
Eloquent Temporary Tags
Auto-expiring tags with additional payload data on any eloquent model.
Built with :heart: for every smart Laravel developer
Installation:
Then you can publish and migrate to create the needed tags table
Sample Application in laravel 8:
https://github.com/imanghafoori1/laravel-tasks
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:
-
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:
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 method
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
andgetTable
methods on it.
Example:
1- Tag a user until tomorrow
2- After an hour the tag is still active, so:
3- After a week, the tag gets expired so:
Getting payload data:
There are 2 ways to get payload data :
You can use getPayload
method
or You can act like eloquent attributes :
Deleting tags:
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:
These methods just do what they say:
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.
Now you can perform these queries:
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:
Example:
When you want to send a notification to users, just 24 hours before their VIP account gets finished, so that they can charge.
Absence of tags:
Example:
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:
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:
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):
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.
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.
: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.
: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:
Laravel Microscope
:gem: It automatically find bugs in your laravel app
Laravel HeyMan
:gem: 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"
All versions of laravel-temp-tag with dependencies
illuminate/database Version ^5.5|^6.0|^7.0|^8.0|^9.0|10.*
illuminate/events Version ^5.5|^6.0|^7.0|^8.0|^9.0|10.*
illuminate/support Version ^5.5|^6.0|^7.0|^8.0|^9.0|10.*