laravel 同步导致 BelongsToMany::sync() 必须是数组类型,给定字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20180968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:41:53  来源:igfitidea点击:

laravel synch causes BelongsToMany::sync() must be of the type array, string given

phplaraveldata-synchronization

提问by ytsejam

I am trying to synch my tags with the posts table. Two models have belongsToManyrelationships to eachother in the model.

我正在尝试将我的标签与帖子表同步。两个模型belongsToMany在模型中相互关联。

My view is with bootstrap typeahead to autosuggest me tags and get the old values:

我的观点是使用 bootstrap typeahead 自动建议我的标签并获取旧值:

<input type="text" data-provide="typeahead" value="{{ Input::old('tags', $theTags) }} class="typeahead" data-items="1" name="tags" "/> 

iin the controller, I check if a tag is included in the db, otherwise I create db entry for the tag:

在控制器中,我检查数据库中是否包含标签,否则我为标签创建数据库条目:

public function postEdit($postId = null)
    {
        $theTags=array();
        $tags = explode(',', Input::get('tags'));

         //check if tag exists in db        
        foreach ($tags as $key=>$value){
        $dbtag = Tag::where('name', '=', $value)->first();
        array_push($theTags, $dbtag);

        //add to db
        if(!$dbtag){
           $dbtag = new Tag;
           // Update the tag
          $dbtag->name= e(ucwords($value));
          $dbtag->slug  = e(Str::slug($value));
          $dbtag->save(); 
          array_push($theTags, $dbtag);      
           }
         }
        // Update the blog post data
        $post->title  = e(Input::get('title'));

        $author = Author::find(Input::get('author_id'));
        // Was the blog post created?
        if($author->posts()->save($post))
        {
            $post->categories()->sync(Input::get('categories'));
            $post->tags()->sync(Input::get('tags'));

            // Redirect to the new blog post page
            return Redirect::to("admin/blogs/$postId/edit")->with('success', Lang::get('admin/blogs/message.update.success'));
        }

    }

I am getting error of :

我收到以下错误:

Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::sync() must be of the type array, string given, called in /home/ytsejam/public_html/remaker/app/controllers/admin/BlogsController.php on line 273 and defined

Can you show me to get the correctly synch the tags ?

你能告诉我正确同步标签吗?

ps: I tried to add another array $theTags ,I am using array_push on them . when I try with

ps:我尝试添加另一个数组 $theTags ,我正在对它们使用 array_push 。当我尝试

$post->tags()->sync($theTags); 

I am getting illegal offset error:

我收到非法偏移错误:

回答by Manuel Pedrera

Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::sync() must be of the type array, string given

Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::sync() must be of the type array, string given

That error is because you are calling sync()with a string, and it should be an array:

该错误是因为您正在sync()使用字符串进行调用,并且它应该是一个数组:

// Input::get('tags') is a string, this will throw an error
$post->tags()->sync(Input::get('tags'));

Now, the other error.

现在,另一个错误。

ps: I tried to add another array $theTags ,I am using array_push on them . when I try with $post->tags()->sync($theTags);I am getting illegal offset error.

ps:我尝试添加另一个数组 $theTags ,我正在对它们使用 array_push 。当我尝试使用$post->tags()->sync($theTags);时,出现非法偏移错误。

I think this error is happening because you are adding nullvalues to the $theTagsarray, since you are calling array_push()twice.

我认为发生此错误是因为您正在向数组添加null$theTags,因为您调用了array_push()两次。

//check if tag exists in db        
foreach ($tags as $key=>$value) {
    $dbtag = Tag::where('name', '=', $value)->first();
    array_push($theTags, $dbtag); // if $dbtag is null, it's being added too
...

I would try with this:

我会尝试这样做:

$theTags=array();
$tags = explode(',', Input::get('tags'));

//check if tag exists in db        
foreach ($tags as $key => $value) {
    $dbtag = Tag::where('name', '=', $value)->first();

    //add to db
    if ( ! $dbtag) {
        $dbtag = new Tag;
        // Update the tag
        $dbtag->name= e(ucwords($value));
        $dbtag->slug  = e(Str::slug($value));
        $dbtag->save(); 
    }

    // Now that you have checked if it's null and created it
    // you can add it safely to the array
    array_push($theTags, $dbtag);      
}

and then, try to call sync()with the array again: $post->tags()->sync($theTags);

然后,再次尝试sync()使用该数组进行调用:$post->tags()->sync($theTags);