无法存储有问题的标签:给定的字符串
can't store tags with question : string given
嗨,我是一个 laravel 初学者,有一个 API 和 laravel 8
我想在我的项目中存储带有问题的标签,并且问题和标签之间的关系是多对多多态的(多态因为我有另一个表并且它们也有标签字段)
在我的 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
我也用过 sync()
,但是标签没有存储在数据库中
我和邮递员一起尝过
块引用
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');
}
感谢您的帮助和关注,我真的需要解决这个问题
尝试attach()
,但您需要传递标签的 ID,而不是标签本身。
如果用户只是在标签之间进行选择,您可以将他选择的标签的 ID 作为数组与请求中的问题一起发送,然后将其传递给附加方法
$question->tags()->attach($request->tagsIds);
如果用户创建了标签,您可以收集新的 ID 并将它们传递给附件,就像这样
$tags = explode(",", $request->tag);//separate tags
$tagsIds = [];
foreach($tags as $tag)
{
array_push($tagsIds,Tag::create(['theTagColumn ex: name'=>$tag])->id);
}
$question->tags()->attach($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
]);
}
嗨,我是一个 laravel 初学者,有一个 API 和 laravel 8
我想在我的项目中存储带有问题的标签,并且问题和标签之间的关系是多对多多态的(多态因为我有另一个表并且它们也有标签字段)
在我的 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
我也用过 sync()
,但是标签没有存储在数据库中
我和邮递员一起尝过
块引用
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');
}
感谢您的帮助和关注,我真的需要解决这个问题
尝试attach()
,但您需要传递标签的 ID,而不是标签本身。
如果用户只是在标签之间进行选择,您可以将他选择的标签的 ID 作为数组与请求中的问题一起发送,然后将其传递给附加方法
$question->tags()->attach($request->tagsIds);
如果用户创建了标签,您可以收集新的 ID 并将它们传递给附件,就像这样
$tags = explode(",", $request->tag);//separate tags
$tagsIds = [];
foreach($tags as $tag)
{
array_push($tagsIds,Tag::create(['theTagColumn ex: name'=>$tag])->id);
}
$question->tags()->attach($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
]);
}