Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel foreach input all update fails because of form token

I recently found my problem what is causing my form error.

I create a form post and loop throuh them

public function update()
    {

        $input = Input::all();

        foreach ($input as $key => $value) {

            $update = Setting::find($key);
            $update->value = $value;
            $update->save();
        }

        return Redirect::back();
    }

The problem is i get the following error

Creating default object from empty value

Because the token is included in the form post what Laravel automaticly renders to a form

if i stop using Laravel form open and use the html form tag it all works fine.

Is there any way to bypass this with laravel form open or should i use the html form tag?

like image 884
Web Student Avatar asked Nov 03 '13 09:11

Web Student


2 Answers

change

 $input = Input::all();

to

 $input = Input::except('_token');
like image 92
Laurence Avatar answered Oct 18 '22 03:10

Laurence


Make sure your $update = Setting::find($key); returns a valid object, because, that error should be triggered when $update is NULL or not defined and you are trying to use this in your code

$update->value = $value;

This warning Creating default object from empty value occurs when E_STRICT is on in the system, but this is not the real problem, instead, you are not getting the desired result, most probably Setting::find($key) is not getting the thing you asked for and creating a new Setting object instead, check your model and make sure you are passing the right value in ::find($key), key should be primary key.

Update : Also remember, when your using

$input = Input::all();
foreach ($input as $key => $value) { ... }

In this case, $key will be the name of your input/field used in the form and it may contain hidden _token field but _token is probably not available in the database as a field/column. So, you may try to get everything except _token

$input = Input::except('_token'); // get everything without _token

to get everything without _token field (but not sure if this solves the problem or not).

like image 37
The Alpha Avatar answered Oct 18 '22 03:10

The Alpha