如何解决无法存储带有问题的标签:给出的字符串
嗨,我是 Laravel 初学者,并且拥有 Laravel 8 的 API
我想在我的项目中存储带有问题的标签,问题和标签之间的关系是多态的(poly 因为我有另一个表而且它们也有标签字段)
在我的 QuestionController()
中,当我想存储问题和标签时,出现此错误:
"Argument 1 passed to Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany::save() must be an instance of Illuminate\\Database\\Eloquent\\Model,string given
当我使用 save
而不是 savemany()
时会出现此错误:
"message": "Argument 1 passed to Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany::save() must be an instance of Illuminate\\Database\\Eloquent\\Model,array given
我和邮递员一起品尝了
块引用
而 method
是这样的:
public function store(StoreQuestionRequest $request)
{
$validatedData = $request->all();
////Because there is no need storage images and question codes
$question = Question::create($validatedData); //store question
//store tags
$tag = new Tag();
$tags = explode(",",$request->tag);//separate tags
$tag['tag'] = $tags;
$question->tags()->savemany($tags);
return response()->json([
'success'=>true,'message'=> 'successfully','data'=>$question
]);
}
所以如果您需要了解 relations
,这些是我的模型:
Question
:
public function tags(){
return $this->morphToMany(Tag::class,'taggable');
}
Tag
:
public function questions(){
return $this->morphedByMany(Question::class,'taggable');
}
感谢您的帮助和关注,我真的需要解决这个问题
解决方法
尝试 --------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
AddMyString 'AddMyString'
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
[\s\S\r]* any character of: whitespace (\n,\r,\t,\f,and " "),non-whitespace (all
but \n,'\r'
(carriage return) (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
) end of look-ahead
但您需要传递标签的 id,而不是标签本身。
如果用户只是在标签之间进行选择,您可以将他选择的标签的 id 作为数组与请求中的问题一起发送,然后将其传递给 attach 方法
attach()
如果用户创建了标签,您可以收集新的 ID 并将它们传递给附件
$question->tags()->attach($request->tagsIds);
希望这对你有用
,解决了:
public function store(StoreQuestionRequest $request)
{
$validatedData = $request->all();
////Because there is no need storage images and question codes
$question = Question::create($validatedData); //store question
//store tags
$tagNames = explode(",",$request->tag);//separate tags
$tagIds = [];
foreach($tagNames as $tagName)
{
$tag = Tag::firstOrCreate(['tag'=>$tagName]);
if($tag)
{
$tagIds[] = $tag->id;
}
}
$question->tags()->sync($tagIds);
return response()->json([
'success'=>true,'message'=> 'successfully','data'=>$question
]);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。