我在创建允许人们编辑个人资料的页面方面遇到问题,问题是我在一个页面上有3个表单(一个用于个人资料设置,第二个用于社交设置,第三个用于帐户设置)和我制作3个控制器来控制每个控制器,问题是我不能同时为一条路线呼叫3个控制器...... 这是我的个人资料.blade.php:
{!! Form::model($user, ['action' => ['ProfileController@updateProfile', 'id' => $user->id], 'method' => 'PUT']) !!}
...Some input to edit username, name, description settings...
{!! Form:close() !!}
{!! Form::model($user, ['action' => ['ProfileController@updateSocial', 'id' => $user->id], 'method' => 'PUT']) !!}
...Some input to set link to their social account...
{!! Form:close() !!}
{!! Form::model($user, ['action' => ['ProfileController@updateSettings', 'id' => $user->id], 'method' => 'PUT']) !!}
...Some input to update password or email...
{!! Form:close() !!}
这是我的web.php(路线):
Route::get('/profile/{urlid}', 'ProfileController@index')->name('profile');
Route::put('/profile/{urlid}', 'ProfileController@updateProfile');
Route::put('/profile/{urlid}', 'ProfileController@updateSocial');
Route::put('/profile/{urlid}', 'ProfileController@updateSettings');
这是我的ProfileController:
public function updateProfile(Request $request, $urlid)
{
$user = User::findOrFail($urlid);
$user->fname=$request->input('fname');
$user->lname=$request->input('lname');
$user->profile_title=$request->input('profile_title');
$user->profile_resume=$request->input('profile_resume');
$user->save();
return view('profile')->with([
'user' => $user
]);
}
public function updateSocial(Request $request, $urlid)
{
$user = User::findOrFail($urlid);
$user->perso_site=$request->input('perso_site');
$user->linkedin=$request->input('linkedin');
$user->twitter=$request->input('twitter');
$user->save();
return view('profile')->with([
'user' => $user
]);
}
public function updateSettings(Request $request, $urlid)
{
$user = User::findOrFail($urlid);
$user->password=$request->input('password');
$user->email=$request->input('email');
$user->save();
return view('profile')->with([
'user' => $user
]);
}
问题是所有这3个表单都在同一页面上,如果我同时调用3个函数,我会收到错误
我只尝试了一个功能,它工作,一切都很好,但有一个功能,只有一个表格正在工作,我希望我会找到一些帮助,我已经差不多2个小时了阻止了这个问题..
总而言之:我在同一页面上有3个表单,我需要这3个函数才能使它们正常工作,但我不能只为一个路由调用3个函数。
答案 0 :(得分:0)
您的路线应该是
Route::get('/profile/{urlid}', 'ProfileController@index')->name('profile');
Route::post('/profile/{profile_id}', 'ProfileController@updateProfile')->name('update_profile');
Route::post('/profile/{social_id}', 'ProfileController@updateSocial')->name('update_social');
Route::post('/profile/{settings_id}', 'ProfileController@updateSettings')->name('update_settings');
你的刀片形式
{!! Form::open(['url' => route('update_profile', ['profile_id' => $user->id]), 'method' => 'post', 'files' => 'true', 'enctype' => "multipart/form-data"]) !!}
//....
{!! Form:close() !!}
{!! Form::open(['url' => route('update_social', ['social_id' => $user->id]), 'method' => 'post', 'files' => 'true', 'enctype' => "multipart/form-data"]) !!}
//....
{!! Form:close() !!}
{!! Form::open(['url' => route('update_settings', ['settings_id' => $user->id]), 'method' => 'post', 'files' => 'true', 'enctype' => "multipart/form-data"]) !!}
//....
{!! Form:close() !!}
在你的功能中
public function updateProfile(Request $request, $urlid)
{
$data = $request->all();
return $data; //check what you pass from the form...
//then after remove return
$user = User::findOrFail($urlid);
if($user)
{
$user->fname=$data['fname'];
$user->lname=$data['lname']
$user->profile_title=$data['profile_title']
$user->profile_resume=$data['profile_resume']
$user->update();
return view('profile')->with([
'user' => $user
]);
}
return "User not found";
}
第一张表格:https://pastebin.com/JznW20iy 你的第二个形式是:https://pastebin.com/NuDTtvFR
和功能
public function updateSocial(Request $request, $urlid)
{
$data = $request->all();
return $data; //check what you pass from the form...
//then after remove return
$user = User::findOrFail($urlid);
if($user)
{
$user->perso_site=$data['perso_site'];
$user->linkedin=$data['linkedin']
$user->twitter=$data['twitter']
$user->update();
return view('profile')->with([
'user' => $user
]);
}
return "User not found";
}
public function updateSettings(Request $request, $urlid)
{
$data = $request->all();
return $data; //check what you pass from the form...
//then after remove return
$user = User::findOrFail($urlid);
if($user)
{
$user->email=$data['email'];
$user->password=$data['newPass']
$user->update();
return view('profile')->with([
'user' => $user
]);
}
return "User not found";
}
答案 1 :(得分:0)
将您的网址分开并设置您的路线,例如:
Route::get('/profile/{urlid}', 'ProfileController@index')->name('profile');
Route::post('/profile/{urlid}', 'ProfileController@updateProfile')->name('update_profile');
Route::post('/profile/social/{urlid}', 'ProfileController@updateSocial')->name('update_social');
Route::post('/profile/settings/{urlid}', 'ProfileController@updateSettings')->name('update_settings');
并在表单刀片中使用路由功能来删除动作URL ...
{!! Form::open(['url' => route('update_profile', ['urlid' => $user->id]), 'method' => 'post', 'files' => 'true', 'enctype' => "multipart/form-data"]) !!}
...Some input to edit username, name, description settings...
{!! Form:close() !!}
{!! Form::open(['url' => route('update_social', ['urlid' => $user->id]), 'method' => 'post', 'files' => 'true', 'enctype' => "multipart/form-data"]) !!}
...Some input to set link to their social account...
{!! Form:close() !!}
{!! Form::open(['url' => route('update_settings', ['urlid' => $user->id]), 'method' => 'post', 'files' => 'true', 'enctype' => "multipart/form-data"]) !!}
...Some input to update password or email...
{!! Form:close() !!}