Done!

Argument 1 passed to BelongsToMany formatSyncList must be of the type array, null given


Problem:

Have following function to update article_tag as shown below:

public function update(Article $article)
{
    $article->update($this->request->all());

    $article->tags()->sync($this->request->input('tags'));

    return Redirect::back();
}

This works fine however when updating the article tag table, following error message is displayed!

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

Solution:

The sync method take an array as parameter. Therefore, you need to ensure that when no input (tags in this case) is submitted, it passes an empty array instead of null value into a sync() method. Modified code is listed below:

public function update(Article $article)
{
    $article->update($this->request->all());

     //Add this additional check. If input tag is null then empty array is used.
    $tags = $this->request->input('tags', []);

    $article->tags()->sync($tags);

    return Redirect::back();
}