由于表单令牌,Laravel foreach 输入所有更新都失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19751576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel foreach input all update fails because of form token
提问by Web Student
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
因为令牌包含在表单中,将 Laravel 自动渲染到表单的内容发布到表单中
if i stop using Laravel form open and use the html form tag it all works fine.
如果我停止使用 Laravel 表单打开并使用 html 表单标签,则一切正常。
Is there any way to bypass this with laravel form open or should i use the html form tag?
有没有办法在打开 Laravel 表单的情况下绕过它,或者我应该使用 html 表单标签吗?
回答by Laurence
change
改变
$input = Input::all();
to
到
$input = Input::except('_token');
回答by The Alpha
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 = Setting::find($key);
返回一个有效的对象,因为当$update
为 NULL 或未定义时应触发该错误,并且您尝试在代码中使用它
$update->value = $value;
This warning Creating default object from empty value
occurs when E_STRICTis 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.
Creating default object from empty value
当E_STRICT在系统中打开时会出现此警告,但这不是真正的问题,相反,您没有得到所需的结果,很可能Setting::find($key)
没有得到您要求的东西并创建一个新Setting
对象,请检查您的模型并确保您在 中传递正确的值::find($key)
,key 应该是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
在这种情况下,$key
将是您input/field
在表单中使用的名称,它可能包含隐藏_token
字段,但_token
可能在数据库中不可用field/column
. 所以,你可以尝试得到所有的东西,除了_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).
获得没有_token
字段的所有内容(但不确定这是否解决了问题)。