2014-10-05 32 views
1

当我将数据集存储在Laravel中时,有时会出现此错误,但尚未找到解决方案。Laravel“不允许”关闭“的序列化”

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

是什么原因造成这种序列化错误:

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; 

这里发生错误时被调用的函数?

回答

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

+1

实际上'返回View :: make('admin/customers/show') - > with($ customer);'不起作用,'OP'使用'withcustomer',但它应该是'withCustomer',所以$ customer变量将在'view'中可用,它是'dynamic method'。 – 2014-10-05 16:46:20

+1

很高兴知道。我修复了我错过的一些小问题。我之前没有看到过'with'的用法,所以我有点迷路。 – 2014-10-05 16:48:06

7

只需更换以下行:

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

与此:

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

此外,您还可以使用这样的事情(重新填充与旧值的形式):

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

view中,您可以使用$errors变量来获取错误消息,所以我F你使用$errors->all(),那么你会得到错误消息的数组,并得到一个特定的错误,你可以尝试这样的事:

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

此外,在下面一行:

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

您需要将动态方法更改为withCustomer而不是withcustomer,因此您可以在view中访问$customer变量。

相关问题