Search code examples
phplaraveldata-synchronization

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


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

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

<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

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

I am getting illegal offset error:


Solution

  • 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:

    // 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.

    I think this error is happening because you are adding null values to the $theTags array, since you are calling array_push() twice.

    //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);