未找到列:1054 Laravel“字段列表”中的未知列“_token”

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

Column not found: 1054 Unknown column '_token' in 'field list' Laravel

phplaravel

提问by DarkFenix

I try to update a record of my table category, but it shows me the error

我尝试更新我的表类别的记录,但它向我显示了错误

Column not found: 1054 Unknown column '_token'

未找到列:1054 未知列“_token”

Route

路线

Route::post('/categorias/edit/{id}', 'CategoryController@update');

Controller

控制器

public function update(Request $request, $id)
{
    $data = request()->all();
    Categoria::where('id', '=', $id)->update($data);
    return redirect()->to('categorias');
}

Model

模型

class Categoria extends Model
{
    protected $table = 'categoria';
    protected $fillable = ['id','codigo','nombre','descripcion','estado'];

Form

形式

{{ Form::open(array('url' => url('categorias/add') , 'class'=>'form-horizontal' , 'id'  => 'formulario' , 'method' => 'POST')) }}

{{ csrf_field() }}
<input id="idcate" name="id" type="hidden" placeholder="" class="form-control input-md" required="">
<input id="txtcodigo" name="codigo" type="text" placeholder="" class="form-control input-md" required="">

<input id="txtnombre" name="nombre" type="text" placeholder="" class="form-control input-md" required="">

<textarea class="form-control" id="txtdescripcion" name="descripcion"></textarea >

<select id="cboestado" name="estado" class="form-control">
      <option value="0">NO ACTIVO</option>
      <option value="1">ACTIVO</option>
</select>
<button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
<button type="submit" class="btn btn-primary">Guardar</button>
{{ Form::close() }}

The records are in a table and I use the same form to edit and to add a new record, only that by clicking the edit button, I change the action

记录在一个表中,我使用相同的表单来编辑和添加新记录,只有通过单击编辑按钮,我才能更改操作

$('#formulario').attr('action', '{{ url("categorias/edit")}}'+ "/"+ $('#idcate').val());

回答by EddyTheDove

Your error comes from

你的错误来自

$data = request()->all();
//which includes '_token'
//coming from csrf_field()

Do instead

改为

$data = request()->except(['_token']);
//same as $request->except('_token');

回答by Rano Paimin

Just change this at your controller

只需在您的控制器上更改它

Categoria::find($id)->update($data)


回答by Mauro Nidola

I disagree with accepted answer.

我不同意接受的答案。

From my point of view it is better to use form model binding with array as name attribute (see How to use Laravel form model binding with array as name attribute?).

从我的角度来看,最好使用带有数组作为名称属性的表单模型绑定(请参阅如何使用带有数组作为名称属性的 Laravel 表单模型绑定?)。

{{ Form::model(['user' => $user], array('class'=>'form-horizontal')) }}
    {{ Form::label('user[email]', 'Email Address') }}
    {{ Form::text('user[email]') }}
{{ Form::close() }}

In the controller you can access user data with the following code without bothering about other data transmitted on submit (eg. _method, _token, submit, ...):

在控制器中,您可以使用以下代码访问用户数据,而无需担心提交时传输的其他数据(例如 _method、_token、提交等):

$request->input('user');

If you want to use this approach, then you also have to update validation code.

如果您想使用这种方法,那么您还必须更新验证代码。

Form model binding in Rails (the other framework I work with) has a far better implementation, the presented approach is the default.

Rails(我使用的另一个框架)中的表单模型绑定有一个更好的实现,所提供的方法是默认方法。