Error writing a collection of checkboxes

0

Live,

I'm using laravel 5.1 and after submitting a form that has a collection of checkboxes

array:3 [▼
  0 => "24"
  1 => "26"
  2 => "32"
]

I write the record and then in a related table I insert the record block

if( $auto->save() )
        {
            $extras = $request['extra'];
            foreach( $extras as $extra)
            {
                $extra = new AutoExtras;
                $extra->auto_id = $auto->id;
                $extra->extra_id = $extra;
                $extra->save();
            }
        }

This would be supposed to work however, it creates the first record by putting the extra_id = 0 and gives the following error:

  

ErrorException in helpers.php line 685:   Method App \ AutoExtras :: __ toString () must return a string value

What is the problem?

    
asked by anonymous 01.02.2018 / 03:25

1 answer

0

The error happens because the same instance variable name of class AutoExtras is the same as the name of the variable that is in foreach , the name of one or the other example :

if( $auto->save() )
{
    $extras = $request['extra'];
    foreach( $extras as $ex )
    {
        $extra = new AutoExtras();
        $extra->auto_id = $auto->id;
        $extra->extra_id = $ex;
        $extra->save();
    }
}
    
01.02.2018 / 14:03