使用Laravel编辑个人资料页面

3
每当用户访问mywebsite.com/profile时,我希望一个表单预先填充他们在注册流程中输入的值。

我已经成功实现了这个功能,请看这里的表单。
<h1><b>Your Profile</b></h1>
<form method="POST" action="/profile/update">
    <div class="form-group hidden">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <input type="hidden" name="_method" value="PATCH">
    </div>
    <div class="form-group {{ $errors->has('name') ? ' has-error' : '' }}">
        <label for="email" class="control-label"><b>Name:</b></label>
        <input type="text" name="name" placeholder="Please enter your email here" class="form-control" value="{{ $user->name }}"/>

        <?php if ($errors->has('name')) :?>
        <span class="help-block">
            <strong>{{$errors->first('name')}}</strong>
        </span>
        <?php endif;?>

    </div>
    <div class="form-group {{ $errors->has('email') ? ' has-error' : '' }}">
        <label for="email" class="control-label"><b>Email:</b></label>
        <input type="text" name="email" placeholder="Please enter your email here" class="form-control" value="{{ $user->email }}"/>

        <?php if ($errors->has('email')) :?>
        <span class="help-block">
            <strong>{{$errors->first('email')}}</strong>
        </span>
        <?php endif;?>

    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-default"> Submit </button>
    </div>
</form>

但是现在我遇到了一个问题:如果用户只点击提交按钮而没有更改任何字段,laravel的内置验证会检查并给出一个错误信息:名称已被占用。这是正确的。所以我不确定如何处理这个问题,但是下面是我的控制器:

<?php

namespace App\Http\Controllers;

use Auth;
use App\User;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

class ProfileController extends Controller
{
    /**
     * Update user profile & make backend push to DB
     * 
    **/

    public function index() {

        /**
         * fetching the user model
         **/
        $user = Auth::user();
        //var_dump($user);

        /**
         * Passing the user data to profile view
         */
        return view('profile', compact('user'));

    }

    public function update(Request $request) {
        /**
         * Validate request/input 
         **/
        $this->validate($request, [
            'name' => 'required|max:255|unique:users',
            'email' => 'required|email|max:255|unique:users',
        ]);

        /**
         * storing the input fields name & email in variable $input
         * type array
         **/
        $input = $request->only('name','email');

        /**
         * fetching the user model
         */
        $user = Auth::user();

        /**
         * Accessing the update method and passing in $input array of data
         **/
        $user->update($input);

        /**
         * after everything is done return them pack to /profile/ uri
         **/
        return back();
    }
}

这是我的路由文件。

// only authenticated users
Route::group( ['middleware' => 'auth'], function() {

    Route::get('/home', 'HomeController@index');

    // practicing using forms for sending data to the DB & populating form fields with DB data
    Route::get('profile', 'ProfileController@index');
    Route::patch('profile/{id}', 'ProfileController@update');

});
1个回答

5

问题是由于唯一性验证规则引起的。您可以通过指定唯一性验证规则来排除当前用户ID。您的代码将如下所示:

 public function update(Request $request) {

  /**
     * fetching the user model
     */
    $user = Auth::user();


    /**
     * Validate request/input 
     **/
    $this->validate($request, [
        'name' => 'required|max:255|unique:users,name,'.$user->id,
        'email' => 'required|email|max:255|unique:users,email,'.$user->id,
    ]);

    /**
     * storing the input fields name & email in variable $input
     * type array
     **/
    $input = $request->only('name','email');



    /**
     * Accessing the update method and passing in $input array of data
     **/
    $user->update($input);

    /**
     * after everything is done return them pack to /profile/ uri
     **/
    return back();
}

这是Laravel文档相关链接:https://laravel.com/docs/5.1/validation#rule-unique

。该链接与IT技术有关,涉及Laravel验证规则中的唯一性验证。


我已经在验证规则的末尾添加了$user->id,以忽略检查唯一性的行。 - Bishnu Bhusal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接