问题描述
已登录的用户可以导航到settings.blade.php并查看其当前的电子邮件,密码,名称等.通过此表单,他们应该能够更新其电子邮件,密码,名称等.
Logged in users can navigate to settings.blade.php and see their current email, password, name etc. From this form they should be able to update their email, password, name etc.
我有表格,它成功地从数据库中提取用户数据.现在,当用户更新(更改)诸如名称"之类的表单字段时,我需要相应地更新数据库.我只是找不到形式为method ="POST"的动作应该是什么?
I have the form and it successfully pulls in user data from database. Now, when user updates (changes) a form field, such as 'name', I need the database to be updated accordingly. I just can't find what the form method="POST" action should be?
web.php
Route::get('settings', 'SettingsController@edit')->name('settings');
Route::post('settings/update', 'SettingsController@update')->name('settings_update');
setting.blade.php
setting.blade.php
This page has a form so that users can update their name and email. Form POST method is missing because everything I try is not working
<form method="POST" action="WHAT GOES HERE?" enctype="multipart/form-data">
SettingsController.php
SettingsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SettingsController extends Controller
{
/**
* Show the form for editing the specified resource.
*
* @return \Illuminate\Http\Response
*/
public function edit()
{
$user = auth()->user();
return view('user_admin.settings', compact('user'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required',
]);
$user = auth()->user();
$user->first_name = $request->get('first_name');
$user->last_name = $request->get('last_name');
$user->email = $request->get('email');
$user->about_me = $request->get('about_me');
$user->save();
return redirect('settings')->with('success', 'Settings updated!');
}
}
推荐答案
当我启动laravel时,我也遇到了这个问题,后方法命名不起作用,然后尝试以下代码:
When i started laravel i also faced that problem, post method naming was not working, then i try something like below code:
Route::get('settings', 'SettingsController@edit')->name('settings');Route::post('settings', 'SettingsController@update');
Route::get('settings', 'SettingsController@edit')->name('settings');Route::post('settings', 'SettingsController@update');
然后在刀片文件中可以这样写
Then in blade file you can write like,
<form method="POST" action="{{ route('settings') }}" enctype="multipart/form-data">
<form method="POST" action="{{ route('settings') }}" enctype="multipart/form-data">
让我知道是否需要更多帮助.
Let me know if you need more help.
这篇关于form method ="POST"行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!