I try to update a record of my table category, but it shows me the error
Column not found: 1054 Unknown column '_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());
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');
Just change this at your controller
Categoria::find($id)->update($data)
The answers to this question are wrong about the proper procedure, what you should do is filter the data received by the variable $request, to do so, create an array and capture the fields of interest.
This is the basics, I personally would include several other validations, I foresee many things when dealing with security, but it should be enough for you to update without major problems:
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With