【问题标题】:Patch update Authenticated User in Laravel补丁更新 Laravel 中的 Authenticated User
【发布时间】:2015-08-26 02:11:51
【问题描述】:

我正在尝试将个人资料信息编辑/更新到我的用户表中。

我的想法是让经过身份验证的用户能够在用户表中编辑自己的个人资料。

在注册过程中,您只需要填写几个特定项目(用户名、姓名、姓氏、电子邮件、密码),但我还在用户表中添加了几个额外的列(城市、国家、电话、推特,脸书)。

我有一个个人资料用户页面 (route= '/profile'),其中显示了所有信息。当然所有注册时不需要的栏目都不填:

我还有一个编辑页面,其中需要添加信息的列是可编辑的:

这是此 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”页面,除非我从表单中删除“路线”和“方法”。 我不断收到此错误:

谁能指导我应该如何准确触发 PATCH 方法?

【问题讨论】:

    标签: php sql-update laravel-5 patch blade


    【解决方案1】:

    将您的表单开始标记更改为

    {!! 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']) !!}
    

    【讨论】:

    • 如果我尝试得到这个错误:Routes.php 第 23 行中的 ErrorException:尝试获取非对象的属性 ==> 第 23-25 行:Route::bind('user/' .$user->id, function($id) { $user = App\User::find($id)->first(); });
    • 好的,我通过添加这样的括号解决了它说意外的问题:Route::patch('user/{user}', ['as' => 'profile.patch', '使用' => 'ProfileController@update']);虽然现在当我按下保存配置文件时,它只是转到 URL:Localhost/user/1 并在网站上说“禁止访问”。
    【解决方案2】:

    你需要改变:

    {!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
    

    进入:

    {!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
    

    目前,您在表单中创建 URL 并将 User 对象转换为 Json - 这就是它不起作用的原因。

    【讨论】:

    • 如果我尝试得到这个错误:Routes.php 第 23 行中的 ErrorException:尝试获取非对象的属性 ==> 第 23-25 行:Route::bind('user/' .$user->id, function($id) { $user = App\User::find($id)->first(); });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多