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

时间:2013-11-24 21:03:26

标签: php laravel data-synchronization

我正在尝试将我的标签与posts表同步。两个模型在模型中彼此具有belongsToMany个关系。

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

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

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

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'));
        }

    }

我收到错误:

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

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

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

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

我收到了非法的偏移错误:

1 个答案:

答案 0 :(得分:3)

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

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

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

现在,另一个错误。

  

ps:我试图添加另一个数组$ theTags,我正在使用array_push   他们。当我尝试使用$post->tags()->sync($theTags);时,我会收到非法的偏移错误。

我认为发生此错误的原因是您要向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
...

我会尝试这个:

$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);      
}

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