我尝试使用laravel中的表单外观更新用户对象,提交后我收到错误:
ErrorException in Grammar.php line 102:
Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array,
string given, called in
C:\Laravel Projects\ExpenseTool\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 105 and defined
我的要求:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class UserRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|min:3',
'email'
];
}
}
我的HomeController索引:
public function index()
{
$user = Auth::user();
return view('Expenses.home', compact( 'user'));
}
我的表格在Expenses.home中:
{!! Form::model($user, ['method' => 'PUT', 'action' => ['HomeController@update', $user->id]]) !!}
<div class="form-group">
{!! Form::label('Name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('Email', 'Email:') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
我的路线:
Route::resource('home', 'HomeController', ['only' => ['update']]);
我的HomeController @ update:
public function update($id, UserRequest $request)
{
$loggedUser = Auth::user();
$userDB = User::get($id);
$username = $request->input('name');
$userDB->name = $username;
$userDB->update();
return redirect('home');
}
我做错了什么?
答案 0 :(得分:1)
你必须改变
`public function update($ id,UserRequest $ request) { $ loggedUser = Auth :: user();
// you are cool to use findOrFail
$userDB = User::find($id);
$username = $request->input('name');
$userDB->name = $username;
$userDB->update();
return redirect('home');
}`
导致错误的原因是get()
方法要求您传递要从数据库中检索的列数组