2

I have something strange happening which I can't understand and wondered if anyone could help me work out what's going on.

I have 2 tables (assessors, processes) and a pivot (process_assessor). I am trying to update the pivot with an assessor's processes so I have a form which when submitted is passed to this method...

public function updateProcesses( $id )
{
    $assessor = $this->model->find( $id );
    return $assessor->processes()->sync( $this->request->get( 'process_id' ) );
}

This works fine however when updating a user to have no processes I get an error message...

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

However, (and this is the weird bit) I also have a user profile module which uses exactly the same way to 'sync' with the pivot table however when I submit that form with all options for that particular module unchecked then it works, the user is detached from them in the pivot.

What am I doing wrong? Any ideas?

bencarter78
  • 3,555
  • 9
  • 35
  • 53

1 Answers1

6

Simply cast to an array:

return $assessor->processes()->sync( (array) $this->request->get('process_id') );

Alternatively you can use empty array as a default value for get():

return $assessor->processes()->sync( $this->request->get('process_id', []) );

however 1st option is more reliable.

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157