微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法存储带有问题的标签:给出的字符串

如何解决无法存储带有问题的标签:给出的字符串

嗨,我是 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

我也使用了 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');
    }

感谢您的帮助和关注,我真的需要解决这个问题

解决方法

尝试 -------------------------------------------------------------------------------- ( 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?