gpt4 book ai didi

php - 使用 laravel 编辑个人资料页面

转载 作者:可可西里 更新时间:2023-11-01 00:40:53 24 4
gpt4 key购买 nike

每当用户访问 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 的内置验证检查并给我一条错误消息 The name has already been taken. 这是真的。所以我不确定如何处理这个问题,但下面是我的 Controller

<?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');

});

最佳答案

问题是由于唯一的验证规则。您可以通过指定唯一验证规则来排除当前用户 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();
}

这里是这个 https://laravel.com/docs/5.1/validation#rule-unique 的 laravel 文档链接

关于php - 使用 laravel 编辑个人资料页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com