首页 > 解决方案 > 如何在 Laravel 上以 html 形式使用 PATCH 或 PUT 方法

问题描述

我正在从事 laravel 项目的实践。使用 PUT / PATCH 以 html 形式发送数据时出现错误。我阅读了 laravel 文档,并在 html 中输入了这个代码行“@method('PUT')”但是这个错误仍然出现。我可以修复这个错误吗?谢谢你。

web.php 文件

Route::get('/', function () {
return view('welcome');
});

 Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/course', 'CourseController@index');


Route::post('/course','CourseController@store');

Route::get('/profile','ProfileController@index');

Route::get('/about','HomeController@about');

Route::get('/contact','HomeController@contact');

Route::get('/organization','OrganizationController@index');
Route::get('/organization/create','OrganizationController@create');
Route::post('/organization','OrganizationController@store');
Route::get('/organization/{organizationId}','OrganizationController@show');
Route::get('/organization/{organizationId}/edit','OrganizationController@edit');
Route::patch('/organization/{organizationId}','OrganizationController@update');

OrganizationController(本案例更新功能)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class OrganizationController extends Controller
{
public function index(){

    $organizations = \App\Organization::All();

    return view('organization.index',compact('organizations'));
}

public function create(){

    return view('organization.create');
}

public function store(){

    $data = request()->validate([
        'organizationName' => 'required',
        'password' => 'required',
        'organizationType' => 'required',
        'email' => 'email',
        'telephoneNo',
        'webpage',
        'organizationEmail'
    ]);

    \App\Organization::create($data);
    return redirect('/organization');
}

public function show($organizationId){
    $organization = \App\Organization::findOrFail($organizationId);

    return view('organization.show',compact('organization'));
}

public function edit(\App\Organization $organization){

    return view('organization.edit',compact('organization'));
}

public function update(\App\Organization $organization){

    $data = request()->validate([
        'organizationName' => 'required',
        'password' => 'required',
        'organizationType' => 'required',
        'email' => 'email',
        'telephoneNo',
        'webpage',
        'organizationEmail'
    ]);

    $organization->update($data);
    return redirect('/organization');
}
}

编辑刀片.php

<h1>Edit Organization</h1>

<form action="/organization/{{ $organization->id }}" method="POST">


    <input type="hidden" name="_method" value="PATCH">
    @csrf
    <div>
        <label for="name">Organization Name</label>
    <input type="text" name="organizationName" autocomplete="off" value="{{ 
$organization>organizationName }}">
        @error('name')<p>{{ $message }}</p> @enderror
    </div>
    <div>
        <label for="organizationType">Organization Type</label>
        <input type="text" name="organizationType" autocomplete="off" value="{{ 
$organization>organizationType }}">
        @error('organizationType')<p>{{ $message }}</p> @enderror
    </div>
    <div>
        <label for="password">Organization Password</label>
        <input type="password" name="password" autocomplete="off" value="{{ $organization->password 
}}">
        @error('password')<p>{{ $message }}</p> @enderror
    </div>
    <div>
        <label for="name">Telephone Number</label>
        <input type="text" name="telephoneNo" autocomplete="off" value="{{ 
$organization>telephoneNo }}">
        @error('telephoneNo')<p>{{ $message }}</p> @enderror
    </div>
    <div>
        <label for="webpage">Web Site</label>
        <input type="text" name="webpage" autocomplete="off" value="{{ $organization->webpage }}">
        @error('webpage')<p>{{ $message }}</p> @enderror
    </div>
    <div>
        <label for="organizationEmail">E-Mail</label>
        <input type="email" name="organizationEmail" autocomplete="off" value="{{ 
$organization>organizationEmail }}">
        @error('email')<p>{{ $message }}</p> @enderror
    </div>

    <button>Save Organization</button>
</form>

我尝试编辑组织这个页面 http://localhost:8000/organization/1/edit 点击保存,我看到了这个错误。 错误页面

谢谢你。

标签: phplaravelmodel-bindinglaravel-6

解决方案


在你的form标签后面加上这一行:

@method('PATCH')

如果你想放,让它放

 @method('PUT')

推荐阅读