无法在帖子请求

时间:2018-03-09 06:07:53

标签: laravel laravel-5 laravel-5.2

我成功点击路线并获取请求数据。当我在提交表单后在我的控制器中请求('描述')时,它会显示描述。但由于某种原因,它没有在我的数据库中更新。

public function personal(User $user){                            
    // Validate form
    $this->validate(request(), [
        'description' => 'max:107',
        'country' => 'required|regex:/^[A-Z]+$/' // Uppercase letters
    ]);

    $user->update([
        'description' => request('description'),
        'country' => request('country')
    ]);

    return redirect()->back();
}

-

// controller.php
    Route::patch('/users/{user}/personal', 'UsersController@personal');

    // html

    <form method="POST" action="/users/{{$user->id}}/personal">
        {{ csrf_field() }}
        {{ method_field('PATCH') }}
        <textarea name="description" maxlength="107"></textarea>

        <select name="country">
          ... options here
        </select>

        <button type="submit">Update</button>
    </form>

1 个答案:

答案 0 :(得分:0)

您未在函数中传递请求,请尝试传递$ request object

public function personal(User $user, Request $request)
{                            
    // Validate form
    $this->validate(request(), [
        'description' => 'max:107',
        'country' => 'required|regex:/^[A-Z]+$/' // Uppercase letters
    ]);

    $user->update([
        'description' => $request->description,
        'country' => $request->country
    ]);

    return redirect()->back();
}