This package offers taggable behaviour for your models. After the package is installed the only thing you have to do is add the HasTags
trait to an Eloquent model to make it taggable.
But we didn't stop with the regular tagging capabilities you find in every package. Laravel Tags comes with batteries included. Out of the box it has support for translating tags, multiple tag types and sorting capabilities.
You'll find the documentation on https://spatie.be/docs/laravel-tags.
Here are some code examples:
// create a model with some tags
$newsItem = NewsItem::create([
'name' => 'The Article Title',
'tags' => ['first tag', 'second tag'], //tags will be created if they don't exist
]);
// attaching tags
$newsItem->attachTag('third tag');
$newsItem->attachTag('third tag','some_type');
$newsItem->attachTags(['fourth tag', 'fifth tag']);
$newsItem->attachTags(['fourth_tag','fifth_tag'],'some_type');
// detaching tags
$newsItem->detachTags('third tag');
$newsItem->detachTags('third tag','some_type');
$newsItem->detachTags(['fourth tag', 'fifth tag']);
$newsItem->detachTags(['fourth tag', 'fifth tag'],'some_type');
// syncing tags
$newsItem->syncTags(['first tag', 'second tag']); // all other tags on this model will be detached
// syncing tags with a type
$newsItem->syncTagsWithType(['category 1', 'category 2'], 'categories');
$newsItem->syncTagsWithType(['topic 1', 'topic 2'], 'topics');
// retrieving tags with a type
$newsItem->tagsWithType('categories');
$newsItem->tagsWithType('topics');
// retrieving models that have any of the given tags
NewsItem::withAnyTags(['first tag', 'second tag'])->get();
// retrieve models that have all of the given tags
NewsItem::withAllTags(['first tag', 'second tag'])->get();
// translating a tag
$tag = Tag::findOrCreate('my tag');
$tag->setTranslation('name', 'fr', 'mon tag');
$tag->setTranslation('name', 'nl', 'mijn tag');
$tag->save();
// getting translations
$tag->translate('name'); //returns my name
$tag->translate('name', 'fr'); //returns mon tag (optional locale param)
// convenient translations through taggable models
$newsItem->tagsTranslated();// returns tags with slug_translated and name_translated properties
$newsItem->tagsTranslated('fr');// returns tags with slug_translated and name_translated properties set for specified locale
// using tag types
$tag = Tag::findOrCreate('tag 1', 'my type');
// tags have slugs
$tag = Tag::findOrCreate('yet another tag');
$tag->slug; //returns "yet-another-tag"
// tags are sortable
$tag = Tag::findOrCreate('my tag');
$tag->order_column; //returns 1
$tag2 = Tag::findOrCreate('another tag');
$tag2->order_column; //returns 2
// manipulating the order of tags
$tag->swapOrder($anotherTag);
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
This package requires Laravel 8 or higher, PHP 8 or higher, and a database that supports json
fields and MySQL compatible functions.
You can install the package via composer:
composer require spatie/laravel-tags
The package will automatically register itself.
You can publish the migration with:
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-migrations"
After the migration has been published you can create the tags
and taggables
tables by running the migrations:
php artisan migrate
You can optionally publish the config file with:
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-config"
This is the contents of the published config file:
return [
/*
* The given function generates a URL friendly "slug" from the tag name property before saving it.
* Defaults to Str::slug (https://laravel.com/docs/5.8/helpers#method-str-slug)
*/
'slugger' => null,
];
You'll find the documentation on https://docs.spatie.be/laravel-tags/v4.
Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the laravel-tags
package? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.
If you've found a bug regarding security please mail freek@spatie.be instead of using the issue tracker.
.env.example
to .env
and fill in your database credentials.composer test
.Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
The MIT License (MIT). Please see License File for more information.
本文经授权转自 PHPHub 社区 功能说明 使用最简便的方式,为你的数据模型提供强大「打标签」功能。 项目地址:https://github.com/summerblue/laravel-taggable 本项目修改于 rtconner/laravel-tagging 项目,增加了一下功能: 标签名唯一; 增加 etrepat/baum 依赖,让标签支持无限级别标签嵌套; 中文 slug 拼音自
场景:为了方便开发使用了laravel-admin框架,使用的时候遇到了很多不理解,也解决很多问题,但是单独拿出来,感觉没必要记录,正好遇到了一个结合多个问题的模块,能概括很多问题,就一次性记录一下,方便以后使用。这是一个产品模块,有标签,分类,多选,单选,图片上传等功能。这里不概述如何创建模块 1、添加功能 /** * Make a form builder. *
总结laravel-admin展示用到的基本方法 基础用法 自定义model 当列表数据获取有特定条件或自己写ORM方法时可以用到,支持排序 $grid->model()->select('id','name')->where('status',1)->groupBy('project_id'); $grid->model()->select('id','name')->where('status
1.在在App\Admin\Extensions 增加 CustomExporter.php; <?php namespace App\Admin\Extensions; use Encore\Admin\Grid; use Illuminate\Support\Arr; use Encore\Admin\Grid\Exporters\AbstractExporter; class Cus
场景:创建产品信息,图片会单独存储到good_image表中,其他数据存储到产品表中 1、处理数据前提准备 good 产品表,good_image 产品图片表 生成GoodController, 2、GoodController,from方法接收数据 protected function form() { $form = new Form(new Good());
Laravel 是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。 功能特点 1、语法更富有表现力 你知道下面这行代码里 “true” 代表什么意思么? $uri = Uri::create(‘some/uri’, array(), array(), tr
我需要空间/Laravel权限的帮助。当我试图分配它给我错误哎呀,看起来像出了问题。 错误 Connection.php第761行中的QueryExcema:SQLSTATE[23000]:完整性约束冲突:1048列role_id不能为空(SQL:插入到(,)值(9,))
Laravel 作为现在最流行的 PHP 框架,其中的知识较多,所以单独拿出来写一篇。 简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件。 注册类文件自动加载器 : Laravel通过 composer 进行依赖管理,无需开发者手动导入各种类文件,而由自动加载器自行导入。 创建服务容器:从 bootstrap/ap
简介 Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。 目前,Scout 自带一个 Algolia 驱动;不过,编写自定义驱动很简单, 你可以轻松的通过自己的搜索实现来扩展 Scout。 安装 首先,通过 Composer 包管理器来安装 Scout: composer
简介 Laravel 致力于让整个 PHP 开发体验变得愉快, 包括你的本地开发环境。 Vagrant 提供了一种简单,优雅的方式来管理和配置虚拟机。 Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,而无需在本地机器安装 PHP 、Web 服务器和其他服务器软件。不用担心会搞乱你的操作系统!Vagrant boxes 是一次性的。如果
WebStack-Laravel 一个开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己的网址导航。 部署 克隆代码: git clone https://github.com/hui-ho/WebStack-Laravel.git 安装依赖: composer installphp artisan key:generate 编辑配置: cp .env.example .env ...D