如何在laravel中使用rtconner/laravel-taggable使更新数据可标记,我已经在我的项目中尝试了这个代码,这是仓库的代码
public function updateNews($id, $data)
{
$tags = explode(',', $data['tags']);
unset($data['tags']);
$model = $this->model->where('id', $id)->update($data);
$model->tag($tags);
return $model;
}
这是控制器的代码
public function update(NewsUpdateRequest $request, $id)
{
$data = $this->newsService->getById($id);
$validated = $request->validated();
if (isset($request->thumbnail)) {
$path = public_path() . '/uploads/';
if ($data->thumbnail != '' && $data->thumbnail != null) {
$file_old = $path . $data->thumbnail;
unlink($file_old);
}
$filenew = $validated['thumbnail'];
$update_file = rand() . $filenew->getClientOriginalName();
$filenew->move(public_path('uploads'), $update_file);
$validated['thumbnail'] = $update_file;
}
$userId = $this->newsService->update($id, $validated);
Alert::success(' Berhasil Ubah Data ', ' Silahkan Periksa Kembali');
return redirect()->route('news.index');
}
这是申请表
class NewsUpdateRequest extends FormRequest
{
public function rules()
{
return [
'title' => 'required|max:255|unique:news,title,' . $this->route('news'),
'content' => 'required',
'user_id' => 'required',
'category_id' => 'required',
'tags' => 'required',
'slug' => 'required',
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg',
];
}
public function messages()
{
return [
'title.required' => 'Title is required',
'content.required' => 'Content is required',
'title.unique' => 'The title has already been taken. Try another title.',
];
}
protected function failedValidation(Validator $validator)
{
return Alert::error(' Error Tambah Data ', ' Silahkan Periksa Kembali');
}
}
最后这是刀刃
<div class="mb-3">
<label for="TagNews" class="form-label"><b>Tags</b></label>
<input class="form-control" type="text" data-role="tagsinput" name="tags" value="{{ $arrTags }}" >
@if ($errors->has('tags'))
<span class="text-danger">{{ $errors->first('tags') }}</span>
@endif
</div>
错误说“Call to a member function tag()on int”,有人有使用rtconner/laravel-tagging包更新标签的例子吗?如果你有,请把它放在这个帖子上,谢谢
2条答案
按热度按时间7tofc5zh1#
在
updateNews
方法中,调用update
并将其设置为$model
。从文档:
update方法返回受影响的行数。
所以你调用
->tag($model)
的对象是一个int,而不是一个model。pcww981p2#
我有同样的问题,但我发现这个讨论https://github.com/rtconner/laravel-tagging/issues/101
所以,也许就像这样
注意:还应确保配置
在config/tagging.php中设置为false
您还可以阅读其他用法示例https://github.com/rtconner/laravel-tagging/blob/master/docs/usage-examples.md