我已成功在个人资料 View 中检索并显示身份验证用户个人资料详细信息,但我缺少一些内容(或一些内容)来进行实际更新。
你能帮忙吗?
配置文件.blade.view
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Update your user profile</div>
<div class="panel-body">
{!! Form::open(array('action' => array('HomeController@postProfileEdit', $user->id))) !!}
<div class="form-group">
{!! Form::label('firstName', 'First name:') !!}
{!! Form::text('firstName', $userinfo->first_name, ['class' => 'form-control']) !!}
{!! Form::label('lastName', 'Last name:') !!}
{!! Form::text('lastName', $userinfo->last_name, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Update', ['class'=>'btn primary']) !!}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
路线.php
Route::get('profile', 'HomeController@profile');
Route::post('profile', 'HomeController@postProfileEdit');
HomeController.php - 配置文件()
/**
* Show the application user profile to the user.
*
* @return Response
*/
public function profile()
{
$user = Auth::user();
$userinfo = \Auth::user();
return view('profile')->with(['user' => $user])->with('userinfo', $userinfo);
}
HomeController.php - postProfileEdit(请求$请求)
/**
* Store updates to user profile.
*
* @return Response
*/
public function postProfileEdit(Request $request)
{
$userinfo = Request::all();
$user = new User;
$user->exists = true;
$user->id = Auth::user();
$user->first_name = $userinfo['firstName'];
$user->save();
return redirect()->action('HomeController@profile');
//return Response::make('Your user profile was updated successfully!');
}
requests.php - 用于验证
abstract class Request extends FormRequest {
/**
* The URI to redirect to if validation fails
*
* @var string
*/
protected $redirect = 'users/create';
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'firstName' => 'required|min:3',
'lastName' => 'required|min:3'
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
}
我得到:
HomeController.php 第 170 行中出现 FatalErrorException:未找到类“App\Http\Controllers\updateProfile”
我知道 $updateProfile = new updateProfile;
线路错误,我只是不确定出了什么问题。
希望这是有道理的。
更新:
我在 HomeController.php 中将类从 updateProfile 更改为 User(我正在执行更改的模型的名称)
现在我收到以下消息:
HomeController.php第174行中的ErrorException: undefined variable :userinfo
我不确定是否可以使用 profile.blade.view 中的用户信息(见上文)。
更新
我能够通过修复使其工作(或不会出现错误) HomeController@postProfileEdit(post),完成后返回HomeController@profile(get)。但显然,保存没有完成,因为即使我更改了first_name值,当我返回HomeController@profile(其中检索并显示first_name)时它仍然保持不变。
最佳答案
Class 'App\Http\Controllers\updateProfile' not found
此错误意味着 Laravel 正在当前 App\Http\Controllers 命名空间中查找 updateProfile 类,但找不到它。我猜该类位于不同的命名空间中,因此您需要在 HomeController 文件的开头添加 use 语句,例如:
<?php
namespace App\Http\Controllers;
use Some\Namespace\updateProfile;
关于php - 如何形成帖子(验证和更新我的数据库)- Controller 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210278/