提问者:小点点

未找到列:“字段列表”Laravel中的1054未知列“\u标记”


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

未找到列:1054未知列“\u标记”

路线

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

控制器

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

模型

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

类型

{{ 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() }}

记录是在一个表和我使用相同的形式来编辑和添加一个新的记录只是通过点击编辑按钮我改变了行动

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

共3个答案

匿名用户

你的错误来自

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

做代替

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

匿名用户

只要在你的控制器上改变这个

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

匿名用户

关于正确的过程,这个问题的答案是错误的,您应该做的是过滤变量$request接收到的数据,为此,创建一个数组并捕获感兴趣的字段。

这是基础,我个人会包括其他几个验证,我在处理安全性时预见到很多事情,但它应该足以让您在没有重大问题的情况下进行更新:

use Illuminate\Support\Facades\Auth; use App\User;         
     /**
     * This is just a demo, modify it according to your needs, include Facades\Auth in the header of the controler to search for the logged in user (Auth::user ())
     *
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
         $use = Auth::user()->name;
         $user = User::where([['id', $id], ['name', $use]]);
         $request->validate([
          'name'=>'required',
          'email'=>'required'
         ]);
         $array = array(
          'name' => $request->get('name'),
          'email' => $request->get('email')
         );
         $user->update($array);
         return redirect()->route('home')->with('success','Update successfully');

      }