PHP code example of shamilchoudhury / eloquent-tags
1. Go to this page and download the library: Download shamilchoudhury/eloquent-tags library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Shamil\Tags\Taggable;
class Lesson extends Model
{
use Taggable;
}
// Pass in an array
$lesson->tag(['snow', 'linen']);
// or you can pass in a model
$lesson->tag(\Shamil\Tags\Models\Tag::where('name', 'snow')->first());
// or a collection
$lesson->tag(\Shamil\Tags\Models\Tag::whereIn('name', ['snow', 'linen'])->get());
$lesson->tag(['linen']); // lesson has one tag 'linen'
$lesson->tag(['snow', 'linen']); // lesson now has two tags: 'linen' and 'snow'
$lesson->tag(['navy']); // lesson has three tags
$lesson->tag(['snow', 'SNOW', 'sNoW']); // lesson will be tagged with 'snow' only once
$lesson = Lesson::find(1);
foreach($lesson->tags as $tag) {
echo $tag->name . ' ' ;
// or do other stuff
}
$lesson = Lesson::find(1);
foreach($lesson->tags()->orderBy('count', 'desc')->get() as $tag) {
echo $tag->name . ' ' ; // this will echo tags in decreasing order of their count
}
// withAnyTag()
$lesson = Lesson::withAnyTag(['linen', 'snow', 'navy']);
dd($lesson->get()); // take any lesson that is tagged with any of the provided tags
//withAllTags()
$lesson = Lesson::withAllTags(['yellowgreen', 'navy']);
dd($lesson->get()); // only take lessons that are tagged with all of the provided tags
//withoutTags()
$lesson = Lesson::withoutTags(['snow', 'linen']);
dd($lesson->get()); // only take lessons that are not tagged with all of the provided tags
$lesson = Lesson::find(1);
$lesson->retag(['darkorange']);
// all existing tags are removed and model is tagged with the tags provided
// in other words, model is first detagged and then tagged with the new tags
$lesson->tag(['lightcoral', 'yellowgreen', 'navy']);
$lesson->untag(['lightcoral']);
// $lesson is now tagged with "yellowgreen" and "navy"
$lesson = Lesson::find(1);
$lesson->untag(); // all tags are removed
dd($lesson->tags); // $lesson now has an empty collection of tags
$tags = \Shamil\Tags\Models\Tag::usedGte(2); // tags with count greater than or equal to 2
dd($tags->get()); // all tags that have been used twice or more
// Similarly you can use other scopes
$tags = \Shamil\Tags\Models\Tag::usedGt(2); // tags with count greater than 2
$tags = \Shamil\Tags\Models\Tag::usedLte(2); // tags with count less than or equal to 2
$tags = \Shamil\Tags\Models\Tag::usedLt(2); // tags with count less than 2
sh
php artisan migrate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.