Laravel PATCH可进行更新,但控制器中的验证失败

问题描述 投票:0回答:1

我正在跟Laracast实验室,Tweety提要一起跟踪,直到我撞墙之前一切都很好。

我有一个PATCH可以更新用户个人资料,它可以工作并更新记录,但是验证没有。如果完全完成并且记录很好,则提交表单,但是如果表单验证在我的控制器的服务器端失败,我将得到以下信息:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException该路由不支持GET方法。支持的方法:POST。

我看过这里的FAQ部分,并搜索了已经问过的网络,并尝试了几个步骤来对此进行排序。我尝试过:

  1. 在编辑文件模板和控制器中使用$ user->用户名。

  2. 更改从PATCH到PUT的路线(还有POST,只需使用存储即可)

  3. 正在检查用户模型并确保我正在使用'getRouteKeyName',它确实工作正常。
  4. 检查路线中是否有冲突,没有冲突,并且订单/ VERB很好。

有人可以帮忙吗?下面是我的代码。

路线如下:

// Snippet for seeing all the database queries. 
//DB::listen(function($query){var_dump($query->sql, $query->bindings);});

use Illuminate\Support\Facades\Route;

 // Home view, no sign-in required. 
Route::get('/', function () {
return view('welcome');
});

// These routes require and use auth middleware.   
Route::middleware('auth')->group(function(){

// TWeets main page.
Route::get('/tweets/', 'TweetsController@index')->name('home');
// Store a new tweet
Route::post('/tweets/', 'TweetsController@store');

// Profile area, view and update

// Use the 'name' attribute in route/model binding instead of the primary key = user:name 
Route::get('/profile/{user:username}/', 'ProfileController@show')->name('profile');

// Show form to edit the profile
Route::post('/profile/{user:username}/edit','ProfileController@edit')->name('edit_profile');
//->middleware('can:edit,user'); // Don't forget the wildcard.

// Finally, update the users profile information.   
Route::patch('/profile/{user:username}','ProfileController@update')->name('update_profile');

// Handles the follow/unfollow on a toggle method.
Route::post('/profile/{user:username}/follow/','FollowsController@store');

// Logs you out & directs to the homepage.
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');

});

Auth::routes();

控制器如下:

 /**
 * Update the users profile after validation
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(User $user)
{
        $validated = request()->validate([
            'name'=>'required',
            'username'=>'required',
            'password'=>'required',
            'password_confirmation'=>'required',
        ]);

         $user->update($validated);

    // Redirect to the tweets page which is called 'home'.
    return redirect(route('profile', $user->username));
}

模板:

<x-app>

<form class="w-full" method="POST" action="{{route('update_profile',$user)}}">

    @method('PATCH')
    @csrf


    <h2 class="text-xl mb-2 py-4 font-bold">Edit profile for {{$user->username}}</h2> 
    <p class="text-sm mb-5 mt-0">Below you an update your profile details.</p>

    <div class="flex flex-wrap -mx-3 mb-6">

        <div class="w-full px-3 mb-6 md:mb-0">
            <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-name">Name</label>
            <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white" id="name" type="text" placeholder="Mike Smith.." name="name" value="{{$user->name}}"/>
        </div>

        @error('name')
            <p>{{$message}}</p>
        @enderror

        <div class="w-full px-3 mb-6 md:mb-0">
            <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-username">Username</label>
            <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white" id="grid-username" type="text" placeholder="MikeSmith1..." name="username" value="{{$user->username}}"/>
        </div>

        @error('username')
            <p>{{$message}}</p>
        @enderror

        <div class="w-full px-3 mb-6 md:mb-0">
            <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-username">Email</label>
            <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-red-500 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white" id="grid-username" type="email" placeholder="[email protected]..." name="email" value="{{$user->email}}"/>
        </div>

        @error('email')
            <p>{{$message}}</p>
        @enderror


        <div class="w-full px-3">
            <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-password">Password</label>
            <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-password" type="password" name="password"/>

            <p class="mb-3 text-red-600 text-xs italic">Make it as long and as crazy as you'd like</p>
        </div>

        <div class="w-full px-3"> {{-- This HAS to be called "password_confirmation" --}}
            <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-password">Password Confirmation</label>
            <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-password_confirmation" type="password" name="password_confirmation"/>
        </div>

        <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 ml-2 mt-4 px-4 rounded">
            Update details
        </button> 

    </div>
</form>
</x-app>

Model:User

 /**
 * Laravel <=6 - use this method, after v6, you can do this on the route itself. 
 * Using this so we can use a user name in the route, instead of the ID Laravel normally uses for route/model binding. Now you can use /profile/mikethornley 
 * This will still enable route/model binding to work and still find the user
 * @return [type] [description]
 */
public function getRouteKeyName()
{
    return 'username';
}
laravel validation controller put
1个回答
0
投票

这是问题:

// Use the 'name' attribute in route/model binding instead of the primary key = user:name 
Route::get('/profile/{user:username}/', 'ProfileController@show')->name('profile');

// Finally, update the users profile information.   
Route::patch('/profile/{user:username}','ProfileController@update')->name('update_profile');

您的路线发生冲突……该帖子最终出现在show()方法中,这就是您收到错误的原因。

如果您想要参数,则需要对该发布路线进行一些更改。例如

Route::patch('/profile-create/{user:username}','ProfileController@update')->name('update_profile');
© www.soinside.com 2019 - 2024. All rights reserved.