不允许序列化 'Closure' - laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23451535/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:28:18  来源:igfitidea点击:

Serialization of 'Closure' is not allowed - laravel

phpmysqllaravellaravel-4

提问by TuGordoBello

I'm trying to create a tuple in my table. The data is from a form or received. My database: enter image description here

我正在尝试在我的表中创建一个元组。数据来自表格或收到。我的数据库: 在此处输入图片说明

View

看法

@extends('plantilla')
@section('contenido')
<div class="formulario">
{{Form::open(array('url' => 'servicio/create', 'files' => true))}}

    <input type="text" class="form-control"  id="direccion" name="dirrecion" placeholder="Dirección" >
    <input type="text" class="form-control"  id="cliente" name="cliente" placeholder="Nombre Cliente" >
    <input type="time" name='horainicio' id='horainicio' min='time' max='time' >
    {{Form::file('pdf', array('title' => 'Search for a file to add'))}}
    {{Form::select('idtecnico', $tecnico_selector)}}
    {{ Form::submit('Guardar', array('class' => 'btn btn-primary ')) }} 
{{Form::close()}}
    </div>  
    @stop

Route

路线

Route::get('servicio/create', function(){
$tecnicos = Tecnico::all();
    $tecnico_selector = array();
    foreach ($tecnicos as $tecnico) {
        $tecnico_selector[$tecnico->idTecnico] = $tecnico->Nombre;
    }
    return View::make('formservicio', array('tecnico_selector' => $tecnico_selector));
 });
 Route::post('servicio/create', array('uses' => 'ServicioController@doCreate'));

Model

模型

class Servicio extends Eloquent{
protected $table = 'Servicio';
protected $primaryKey = 'idServicio';
protected $fillable = array(
                            'Direccion',
                            'RutaPDF',
                            'Completado'
                            );
public function materialUsado(){
    return $this->hasMany('Material_Usado', 'idMaterial_Usado');
}

public function tecnicos(){
    return $this->belongsToMany('Tecnico', 'Servicio_Tecnico', 'Servicio_idServicio', 'Tecnico_idTecnico');
}
    }

Controller

控制器

class ServicioController extends BaseController{
public function doCreate(){
    $rules = array(
                'idtecnico' => 'required',
                'direccion' => 'required',
                'cliente' => 'required'
                );
    $validator = Validator:: make(Input::all(), $rules);

    if($validator->fails()){
        return Redirect::to('servicio/create')
                ->withErros($validator)
                ->withInput();
    }else{
        $input = array(
                'Direccion' => Input::get('direccion'),
                'Cliente' => Input::get('cliente'),
                'Completado' => '0'
                );

        Servicio::create($input);
        /*$servicio = new Servicio;
        $servicio->Direccion = Input::get('direccion');
        $servicio->Cliente = Input::get('cliente');
        $servicio->Completado = '0';
        $servicio->tecnicos->attach($idtecnico);
        $servicio->save();*/

        $path = public_path().'/servicio/'.$servicio->idServicio;
        File::makeDirectory($path, $mode = 0777, true, true); // creamos la carpeta
        $archivoPdf = Input::file('pdf');
        $archivoPdf->move($path, 'servicio');

        /*$servicio->RutaPDF = $path.'/servicio';
        $servicio->save();*/







    }
}
}

When completely changes the form and send it, the following error

当完全改变表单并发送时,出现以下错误

enter image description here

在此处输入图片说明

how can I fix it?

我该如何解决?

回答by Andreas

You have a typo in your controller: ->withErros($validator)- it should be withErrors. The difference is big.

您的控制器中有一个错字:->withErros($validator)- 应该是withErrors. 差别很大。

Laravel converts withErros($validator)into with(['erros' => $validator]), which means trying to put the entire validator object into the session. To put things into the session storage, it needs to be serialized first, which is what is causing the error.

Laravel 转换withErros($validator)with(['erros' => $validator]),这意味着尝试将整个验证器对象放入会话中。要将东西放入会话存储中,首先需要对其进行序列化,这就是导致错误的原因。

回答by erajuan

I had similar problem:

我有类似的问题:

The problem was:

问题是:

  • I was validating an input does not exist.
  • The name of the column(database) is not similar to the name of the input
  • The column(database) is not null
  • 我正在验证一个输入不存在。
  • 列(数据库)的名称与输入的名称不相似
  • 列(数据库)不为空