如何形成post(验证和更新我的数据库)-控制器语法


How to form post (validate and update my database) - controller syntax

我已经设法在配置文件视图中检索并显示了Auth用户配置文件的详细信息,但我缺少一些(或一些(东西来进行实际更新。

你能帮忙吗?

profile.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>

routes.php

Route::get('profile', 'HomeController@profile');
Route::post('profile', 'HomeController@postProfileEdit');

HomeController.php-profile((

/**
 * 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(请求$Request(

/**
 * 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中的FatalErrorException第170行:找不到类"App''Http''Controllers''updateProfile">

我知道$updateProfile=new updateProfile;

线路错了,我只是不确定出了什么问题。

希望这是有道理的。


更新:

我在HomeController.php 中将类从updateProfile更改为User(我正在进行更改的模型的名称(

现在我得到以下消息:

HomeController.php中的ErrorException第174行:未定义的变量:userinfo

我不确定我是否可以使用profile.blade.view中的用户信息(见上文(。


更新

我能够通过修复使其工作(或不出现错误(HomeController@postProfileEdit(张贴(,并返回HomeController@profile完成时(得到(。但显然,保存没有完成,因为即使我更改了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;

基于@jedrzej.kurylo的答案。

我必须假设updateProfile是在app目录中找到的Eloquent模型:

<?php
namespace App;
use Illuminate'Database'Eloquent'Model;
class updateProfile extends Model {
    ...
}

注意到文件顶部的namespace声明了吗?接下来,考虑HomeController.php文件的名称空间:

namespace App'Http'Controllers;

默认情况下,在Laravel中构造类时,它会出现在您正在使用的控制器的当前命名空间中。在这种情况下,它试图找到App'Http'Controllers命名空间中的类updateProfile,我们知道它不存在。

有两种方法可以完成你想要做的事情。首先,在控制器顶部为你包含的每个Model使用一个use语句。

use App'updateProfile;
...

其次,在声明类时指定名称空间:

$updateProfile = new 'App'updateProfile;

任何一种方法都应该为您清除错误。