Laravel“不允许'封闭'序列化”

时间:2014-10-05 15:17:07

标签: php laravel

当我在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;

以下是发生错误时调用的函数:

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);
        }
    }

导致此序列化错误的原因是什么?

2 个答案:

答案 0 :(得分:7)

只需更换以下行:

return Redirect::back()->with('message', $validate);

用这个:

return Redirect::back()->withErrors($validate);

此外,您可以使用类似的内容(使用旧值重新填充表单):

return Redirect::back()->withErrors($validate)->withInput();

view中,您可以使用$errors变量来获取错误消息,因此如果您使用$errors->all(),那么您将获得一系列错误消息并获得特定错误你可以尝试这样的事情:

{{ $errors->first('email') }} // Print (echo) the first error message for email field

另外,在以下行中:

return View::make('admin/customers/show')->withcustomer($customer);

您需要将动态方法更改为withCustomer而不是withcustomer,这样您才能访问$customer中的view变量。

答案 1 :(得分:0)

return Redirect::back()->with('message', $validate);

您正在告诉Laravel将整个验证程序对象序列化为会话。要重定向错误,请使用withErrors方法:

return Redirect::back()->withErrors($validate);

这将从验证器中取出错误消息,并在重定向之前将其闪存到会话。现在你正在尝试将整个类存储在Session中,导致你的错误。

我看到的另一个问题是我认为withcustomer类上没有View方法:

return View::make('admin/customers/show')->withcustomer($customer);

尝试将其更改为with

return View::make('admin/customers/show')->with('customer', $customer);

或确保将Customer部分大写:

return View::make('admin/customers/show')->withCustomer($customer);

另见this question