我只在我的应用程序中实现CRUD
,但UPDATE不会更新数据库I var_dump($user->name)
中的数据,但它不输出任何内容。请查看代码并帮助我。我正在使用Laravel 5.2和资源控制器。
PS:编辑工作正常,例如在此网址http://localhost/pos/users/6/edit?id=6
我可以使用所需数据填充视图字段,但是当我点击数据库时没有更新任何更新。
UserController中:
public function edit($id)
{
$user = User::find($id);
return view('user.update')->with('userToUpdate',$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)
{
$user = User::find($id);
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = bcrypt(Input::get('password'));
$user->save();
return Redirect::to('/users')->with('message', 'User Updated');
}
的UpdateView:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Update</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="PUT" action="{{ url('/users') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{!! $userToUpdate->name !!}">
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{!! $userToUpdate->email !!}">
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password" value="{!! $userToUpdate->password !!}">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn fa-user"></i>Update
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
的usermodel:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
路线:
Route::group(['middleware' => 'web'], function () {
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController@index');
Route::get('/register', function(){
return view('auth.register');
})->middleware('isAdmin');
Route::resource('/users', 'UsersController');
});
答案 0 :(得分:3)
是否因为您在发送表单时没有传递用户ID?尝试将表单操作从{{ url('/users') }}
编辑为{{ url('/users', $userToUpdate->id) }}
。
答案 1 :(得分:3)
来自laravel docs:
https://laravel.com/docs/5.0/routing#method-spoofing
HTML表单不支持PUT,PATCH或DELETE操作。因此,在定义从HTML表单调用的PUT,PATCH或DELETE路由时,您需要向表单添加隐藏的_method字段。
然而,Route :: resource指定更新需要使用PUT方法。因此,您的表单必须是:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/users', $userToUpdate->id) }}">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">