【问题标题】:edit profile dont worked :/ laravel 7编辑个人资料不起作用:/ laravel 7
【发布时间】:2020-10-16 12:14:38
【问题描述】:

我有问题,当我想在 laravel 中编辑我的个人资料时。当我单击按钮更新配置文件时出现此错误:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 PATCH 方法。支持的方法:GET、HEAD。 http://127.0.0.1:8000/profile

编辑.blade.php

@section('内容')
更新配置文件
@方法('补丁') @csrf
@error('名字') {{ $message }} @enderror
{{ $message }} @enderror
{{ $message }} @enderror
@endsection

web.php

使用 Illuminate\Support\Facades\Route; /* |------------------------------------------------- ------------------------- |网络路由 |------------------------------------------------- ------------------------- | |您可以在此处为您的应用程序注册网络路由。这些 |路由由 RouteServiceProvider 在一个组内加载 |包含“web”中间件组。现在创造一些伟大的东西! | */ 路线::get('/', function () { 返回视图(“欢迎”); }); 授权::路由(); Route::get('/home', 'HomeController@index')->name('home'); Route::get('/chats', 'ChatController@index')->name('chats'); Route::get('/messages', 'ChatController@fetchAllMessages'); Route::get('/messages', 'ChatController@sendMessage'); Route::get('/contacts', 'ContactsController@get'); Route::get('/conversation/{id}', 'ContactsController@getMessagesFor'); Route::get('/conversation/send', 'ContactsController@send'); Route::group(['middleware' => 'auth'], function () { Route::get('profile', 'ProfileController@edit')->name('profile.edit'); });

配置文件控制器:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProfileController extends Controller
{
    /**
     * Show the update profile page.
     *
     * @param  Request $request
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function edit(Request $request)
    {
        return view('profile.edit', [
            'user' => $request->user()
        ]);
    }
}

请有人可以帮助解决此错误。我不明白这是什么问题。

passwordChange.blade.php 我创建了这个页面来尝试更改密码是否有效,并且在其他页面中它有效,但是当我在编辑配置文件的一个页面中尝试时,不工作。

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Laravel - Change Password with Current</div>
   
                <div class="card-body">
                    <form method="POST" action="{{ route('profile') }}">
                        @csrf 
   
                         @foreach ($errors->all() as $error)
                            <p class="text-danger">{{ $error }}</p>
                         @endforeach 
  
                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">Current Password</label>
  
                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="current_password" autocomplete="current-password">
                            </div>
                        </div>
  
                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">New Password</label>
  
                            <div class="col-md-6">
                                <input id="new_password" type="password" class="form-control" name="new_password" autocomplete="current-password">
                            </div>
                        </div>
  
                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">New Confirm Password</label>
    
                            <div class="col-md-6">
                                <input id="new_confirm_password" type="password" class="form-control" name="new_confirm_password" autocomplete="current-password">
                            </div>
                        </div>
   
                        <div class="form-group row mb-0">
                            <div class="col-md-8 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Update Password
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection 

当我尝试在不同的页面中执行此操作时,我创建了其他页面 changePassword.blade.php,当我在此页面中更改密码时,它起作用了,当我在离开密码路径等时尝试更新配置文件时...它也有效,但是当我想在一页中更改所有内容时出现此错误

Facade\Ignition\Exceptions\ViewException 未定义变量:用户(查看:/home/mokoch/Bureau/projetabonnementpayant/resources/views/profile/edit.blade.php) http://127.0.0.1:8000/profile

如果有人可以帮我解决这个错误

【问题讨论】:

    标签: php laravel laravel-7


    【解决方案1】:

    问题在于你告诉 Laravel 这是一个带有这一行的补丁请求

    @method('patch')
    

    但是在你的路由文件中你只是在寻找一个 get 方法

    Route::get('profile', 'ProfileController@edit')->name('profile.edit');
    

    如果您是作为补丁发送,那么您需要额外的一行

    Route::patch('profile', 'ProfileController@update');
    

    然后你需要在你的控制器中创建一个更新方法来处理保存逻辑

    public function update(Request $request)
        {
            // Logic to update
        }
    

    【讨论】:

    • 当我尝试在不同的页面中执行此操作时,它有效,我创建了其他页面 changePassword.blade.php 并且当我在此页面中更改密码时它有效,当我尝试在我留下密码时更新配置文件路线等...它也有效,但是当我想在一页中更改所有内容时出现此错误
    【解决方案2】:

    您的路线中的这一行表示它只是一个 GET 请求

    Route::get('profile', 'ProfileController@edit')->name('profile.edit');
    

    您的表单显示 method="POST"

    您可以将您的路线更改为“任何”,这将允许获取和发布

    Route::any('profile', 'ProfileController@edit')->name('profile.edit');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多