php-Laravel 5.6中的PATCH请求
作者:互联网
对于我的每个登录用户,他们在“设置”表中都有一行,它为我提供了有关用户使用我的Web应用程序时所需的各个设置的更多信息.
好的,在Laravel 5.6中,我希望用户能够更新设置.但是,如果它们是首次登录的,可能就不存在了,我不知道PATCH请求在尝试更新该行之前是否检查了该行?
无论哪种方式,这都是我的设置模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use \App\User;
class Settings extends Model
{
protected $connection = "mysql";
protected $table = "settings";
public function user() {
return $this->belongsTo(User::class);
}
}
好的,这对我来说很有意义.在我的settings.blade.php中,我的表单指向/ settings:
<form action="/settings" method="POST">
@csrf
然后在我的SettingsController.php中,我有以下内容:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \App\Settings;
class SettingsController extends Controller
{
//
public function index() {
return view('settings');
}
public function patch() {
Settings::patch(request([
'font',
'updates',
'font-size',
'hide-intro',
'hide-commentary',
'night-mode'
]));
}
}
最后是我的/routes/web.php:
Route::get('/settings','SettingsController@index');
Route::patch('/settings', 'SettingsController@patch');
好的,这样对我来说应该起作用,我知道没有验证,但是我可以稍后再进行验证.但是我收到以下错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
我该怎么做才能解决此问题?我会以错误的方式处理吗?
解决方法:
您需要传递方法动词.在您的表单中添加
<input name="_method" type="hidden" value="PATCH">
标签:laravel-5-6,laravel,php 来源: https://codeday.me/bug/20191025/1927202.html