Error saving a collection of checkboxes

Asked

Viewed 12 times

0

Long live,

I am using Laravel 5.1 and after submitting a form that has a collection of checkboxes

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

I save the record and then enter the block of records into a related table

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, creates the first record by placing 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’s the matter?

1 answer

0


The error happens because, the same class instance variable name AutoExtras is the same name as the variable in foreach, just change 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();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.