对于我的每个登录用户,他们在“设置”中都有一行' table,它提供了有关用户使用我的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
我该怎么做才能解决这个问题?我是怎么回事呢?
答案 0 :(得分:2)
您需要pass
方法verb
。在表单中添加
<input name="_method" type="hidden" value="PATCH">
答案 1 :(得分:2)
在表格内使用以下method_field()
{{ method_field('PATCH') }}