我添加了spatie / laravel-tags插件https://docs.spatie.be/laravel-tags/v2/basic-usage/using-tags 到我的Laravel 5.7应用中,并将其添加到我的一个模型中:
<?php
namespace App;
use DB;
...
use \Spatie\Tags\HasTags;
class Vote extends MyAppModel
{
use HasTags;
protected $table = 'votes';
protected $primaryKey = 'id';
...
我成功地在初始化播种机中添加了一些数据,但是我没有找到如何检索相关标签列表的方法 投票对象:
$vote = Vote::find($vote_id);
$relatedTags = $vote::Tags([])->get(); // That fails
已修改块:
1)我想在播种机数据库/seeds/votesInitData.php中添加一些inin数据:
<?php
use Illuminate\Database\Seeder;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
//use Illuminate\Database\Eloquent\Model;
use App\Vote;
use \Spatie\Tags\HasTags;
class votesInitData extends Seeder
{
use HasTags;
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$lNewVote= Vote::create([
'id' => 1,
'name' => 'Question?',
'slug' => 'question',
'description' => 'Still ...',
'creator_id' => 3,
'vote_category_id' => 1,
'is_quiz' => false,
'is_homepage' Call to undefined method App\Tag::findOrCreate()
=> true,
'ordering' => 1,
'status' => 'A',
'image' => 'tobe.png',
// 'tags' => ['Hamlet', 'William Shakespeare', 'Drama', ' William Shakespeare '],
]);
$tagWithType = Tag::findOrCreate('headline', 'newsTagType');
$lNewVote->attachTag($tagWithType);
我得到了错误:
: Class 'Tag' not found
我是否想添加更多声明?我包括了HasTags。
2)这个
$activeTag = Tag::where('slug', $tag_slug)->first();
不起作用,如我在sql-trace中所见:
SELECT *
FROM `tags`
WHERE `slug` = 'thriller' limit 1
但是查看标签表,我看到标签表包含内容
name : {"en": "Steven Spielberg"}
slug : {"en": "steven-spielberg"}
所以什么也没找到!
哪种方法是正确的?
谢谢!
答案 0 :(得分:0)
您应该使用以下命令将 Tag 类添加到配置文件中:
php artisan vendor:publish --provider="Spatie\Tags\TagsServiceProvider" --tag="tags-config"
并且结尾足以调用海湾名称:Tag::where('slug', $tag_slug)->first()->name;