php - Laravel 8 没有从包“\Conner\Tagging\Taggable;”中找到“标签”
问题描述
当我想从头开始创建一个新标签时,该代码可以完美运行,但$skillsQuery->count() > 0
在 if 语句中输入时。它打印...
方法 Illuminate\Database\Eloquent\Collection::tag 不存在。
如何使用此软件包更新标签?
控制器
<?php
public function storeSkills(Request $request)
{
$id = auth()->user()->id;
$skillsQuery = Skill::where('created_by', $id)->get();
// If skill exists
if ($skillsQuery->count() > 0) {
$input = $request->all();
$tags = explode(", ", $input['name']);
// $skill = Skill::create($input);
$skillsQuery->tag($tags);
$skillsQuery->created_by = $id;
if ($skillsQuery->save()) {
return redirect()->route('profile')->with('success', 'Skills updated successfully');
} else {
return redirect()->route('profile')->with('error', 'Error updated your Skills!');
}
} else {
$input = $request->all();
$tags = explode(", ", $input['name']);
$skill = Skill::create($input);
$skill->tag($tags);
$skill->created_by = $id;
if ($skill->save())
return redirect()->route('profile')->with('success', 'Skills stored successfully');
else {
return redirect()->route('profile')->with('error', 'Error storing your Skills!');
}
}
}
解决方案
调用->get()
a的结果Illuminate\Database\Query
是您将收到一个Illuminate\Database\Collection
不包含->tag()
方法的 a 实例。即使它是一个查询(通过删除->get()
),这仍然不起作用,因为您不能从集合中调用关系方法。
相反,如果您循环,skillsQuery
那么您将收到一个Model
对象的实例,然后您可以访问它的函数和/或关系:
$skillsQuery->each(function ($skill) use ($tags) {
$skill->tag($tags); // or perhaps ->retag($tags); here
});
推荐阅读
- haskell - 为什么在 GHCi 中没有给出类型时枚举操作会失败?
- html - 在 NetSuite 中,如何扩展一个 div 以填充整个模板页面(PDF)?
- ruby-on-rails - 使用 Ruby on Rails ActiveRecord 在表上自联接
- excel - 如何将 Excel 表与 Access 链接并防止由于错误的数据类型转换导致的 NULL 值?
- ios - 在后台的预定点播放文本到语音
- javascript - 无法将 npm 升级到 npm@latest Mac
- python - 如何将两个创建的系列相乘以创建第三个系列
- c# - ML.NET 预测纽约出租车票价 - 程序不包含适用于入口点的静态“主要”方法
- prolog - 在 SWI Prolog 中打开与咨询
- java - 通过在 salesforce 中添加潜在客户的 Salesforce REST API