当我在Laravel中存储数据集时,我有时会遇到此错误并且没有找到解决方案.
Serialization of 'Closure' is not allowed
Open: ./vendor/laravel/framework/src/Illuminate/Session/Store.php
*/
public function save()
{
$this->addBagDataToSession();
$this->ageFlashData();
$this->handler->write($this->getId(), serialize($this->attributes));
$this->started = false;
Run Code Online (Sandbox Code Playgroud)
以下是发生错误时调用的函数:
public function store()
{
$data = Input::all();
$validator = array('first_name' =>'required', 'last_name' => 'required', 'email' => 'email|required_without:phone', 'phone' => 'numeric|size:10|required_without:email', 'address' => 'required');
$validate = Validator::make($data, $validator);
if($validate->fails()){
return Redirect::back()->with('message', $validate);
} else {
$customer = new Customer;
foreach (Input::all() as $field => $value) {
if($field == '_token') continue;
$customer->$field = $value;
}
$customer->save();
return View::make('admin/customers/show')->withcustomer($customer);
}
}
Run Code Online (Sandbox Code Playgroud)
导致此序列化错误的原因是什么?
只需更换以下行:
return Redirect::back()->with('message', $validate);
Run Code Online (Sandbox Code Playgroud)
有了这个:
return Redirect::back()->withErrors($validate);
Run Code Online (Sandbox Code Playgroud)
此外,您可以使用类似的东西(使用旧值重新填充表单):
return Redirect::back()->withErrors($validate)->withInput();
Run Code Online (Sandbox Code Playgroud)
在view你可以使用$errors变量获得错误信息,所以如果你使用$errors->all(),你会得到错误信息的数组,让你可以尝试这样一个特定的错误:
{{ $errors->first('email') }} // Print (echo) the first error message for email field
Run Code Online (Sandbox Code Playgroud)
另外,在以下行中:
return View::make('admin/customers/show')->withcustomer($customer);
Run Code Online (Sandbox Code Playgroud)
您需要将动态方法更改为withCustomer不withcustomer,因此您将能够访问$customer您的变量view.