Laravel 中的补丁程序更新经过身份验证的用户
Patch update Authenticated User in Laravel
我正在尝试 edit/update 将个人资料信息添加到我的用户 table。
我的想法是让经过身份验证的用户能够在用户 table 中编辑自己的个人资料 table。
在注册过程中,您只需要填写一些特定的项目(用户名、姓名、姓氏、电子邮件、密码),但我还为用户 table 添加了一些额外的列(城市、国家、phone、推特、脸书)。
我有一个个人资料用户页面 (route= '/profile'),其中显示了所有信息。当然注册时所有不需要填写的栏目都不填:
我还有一个编辑页面,其中需要添加信息的列是 editable:
这是此 editprofile.blade.php 的代码(我尝试发送一个 PATCH 方法):
...
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
<div class="form-group form-horizontal">
<div class="form-group">
{!! Form::label('username', 'Username:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->username}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('email', 'E-mail:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->email}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('name', 'Name:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->name}} {{ $user->lastname}}<p>
</div>
</div>
<br />
<div class="form-group">
{!! Form::label('city', 'City:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('city', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('country', 'Country:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('country', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('phone', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('twitter', 'Twitter link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('twitter', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('facebook', 'Facebook link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('facebook', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
{!! Form::submit('Save Profile', ['class' => 'btn btn-primary']) !!}
</div>
</div>
</div>
</div>
{!! Form::close() !!}
...
这是我的 Http/routes.php:
# Profile
Route::get('/profile', 'PagesController@profile');
Route::get('/profile/edit', 'ProfileController@edit');
Route::bind('user', function($id) {
$user = App\User::find($id)->first();
});
Route::patch('user/{user}', 'ProfileController@update');
我有一个 Http/Requests/UpdateUserRequest.php 来验证请求:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends Request {
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'city' => 'max:30',
'country' => 'max:30',
'phone' => 'max:30',
'twitter' => 'max:30',
'facebook' => 'max:30'
];
}
}
我的 Http/Controllers/ProfileController.php 具有更新功能:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ProfileController extends Controller {
public function edit()
{
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
public function update(UpdateUserRequest $request, User $user)
{
$user->fill($request->all());
$user->save();
return redirect()->view('pages.editprofile')->withUser($user);
}
}
目前我似乎无法再打开我的 'editprofile.blade.php' 页面,除非我从我的表单中删除 'route' 和 'methode'。
我不断收到此错误:
谁能指导我如何准确触发 PATCH 方法?
将表单开始标记更改为
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
你忘记了'->id'
更新
改变你的路线
来自
Route::patch('user/{user}', 'ProfileController@update');
至
Route::patch('user/{user}', 'as' => 'profile.patch', 'ProfileController@update');
以及您的表单开始标记
{!! Form::model($user, ['route' => array('profile.patch', $user->id), 'method' => 'PATCH']) !!}
您需要更改:
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
进入:
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
目前您在表单中创建 URL,其中 User
对象已转换为 Json - 这就是它不起作用的原因。
我正在尝试 edit/update 将个人资料信息添加到我的用户 table。
我的想法是让经过身份验证的用户能够在用户 table 中编辑自己的个人资料 table。
在注册过程中,您只需要填写一些特定的项目(用户名、姓名、姓氏、电子邮件、密码),但我还为用户 table 添加了一些额外的列(城市、国家、phone、推特、脸书)。
我有一个个人资料用户页面 (route= '/profile'),其中显示了所有信息。当然注册时所有不需要填写的栏目都不填:
我还有一个编辑页面,其中需要添加信息的列是 editable:
这是此 editprofile.blade.php 的代码(我尝试发送一个 PATCH 方法):
...
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
<div class="form-group form-horizontal">
<div class="form-group">
{!! Form::label('username', 'Username:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->username}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('email', 'E-mail:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->email}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('name', 'Name:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->name}} {{ $user->lastname}}<p>
</div>
</div>
<br />
<div class="form-group">
{!! Form::label('city', 'City:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('city', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('country', 'Country:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('country', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('phone', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('twitter', 'Twitter link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('twitter', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('facebook', 'Facebook link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('facebook', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
{!! Form::submit('Save Profile', ['class' => 'btn btn-primary']) !!}
</div>
</div>
</div>
</div>
{!! Form::close() !!}
...
这是我的 Http/routes.php:
# Profile
Route::get('/profile', 'PagesController@profile');
Route::get('/profile/edit', 'ProfileController@edit');
Route::bind('user', function($id) {
$user = App\User::find($id)->first();
});
Route::patch('user/{user}', 'ProfileController@update');
我有一个 Http/Requests/UpdateUserRequest.php 来验证请求:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends Request {
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'city' => 'max:30',
'country' => 'max:30',
'phone' => 'max:30',
'twitter' => 'max:30',
'facebook' => 'max:30'
];
}
}
我的 Http/Controllers/ProfileController.php 具有更新功能:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ProfileController extends Controller {
public function edit()
{
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
public function update(UpdateUserRequest $request, User $user)
{
$user->fill($request->all());
$user->save();
return redirect()->view('pages.editprofile')->withUser($user);
}
}
目前我似乎无法再打开我的 'editprofile.blade.php' 页面,除非我从我的表单中删除 'route' 和 'methode'。 我不断收到此错误:
谁能指导我如何准确触发 PATCH 方法?
将表单开始标记更改为
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
你忘记了'->id'
更新
改变你的路线 来自
Route::patch('user/{user}', 'ProfileController@update');
至
Route::patch('user/{user}', 'as' => 'profile.patch', 'ProfileController@update');
以及您的表单开始标记
{!! Form::model($user, ['route' => array('profile.patch', $user->id), 'method' => 'PATCH']) !!}
您需要更改:
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
进入:
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
目前您在表单中创建 URL,其中 User
对象已转换为 Json - 这就是它不起作用的原因。