php - 如何形成帖子(验证和更新我的数据库)- Controller 语法

标签 php laravel laravel-5 eloquent

我已成功在个人资料 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/

相关文章:

php - MySQL。哪里声明。询问

javascript - 使用 PHP 的 AJAX - 不同的结构方式?

php - 使用 offset+limit 在 mySQL 查询中查找结果总数

php - 使用 laravel 在模型中映射字段名称

php - 在本地 Laravel Homestead 服务器上验证自签名证书

php - Laravel 5 - 更改模型文件位置

php - 在不换行的情况下在 div 中显示内容

laravel - 禁用 Laravel 5(不是 5.1)中间件进行测试

apache - 显示存储文件夹中的图像时 Laravel 403 错误

php - Laravel 5.2 高 CPU 长路由