我正在设置用户个人资料更新部分。 但是当我进入个人资料创建页面时,出现以下错误。
此路由不支持
GET
方法。支持的方法:POST
。
我尝试了php artisan route:clear
并检查了所有内容。
但是我无法解决这个问题,所以很高兴有人帮助我。
web.php
Route::get('user/profile','UserController@index')->name('profile.index');
Route::POST('user/profile/create', 'UserController@store')->name('profile.create');
create.blade.php
<form action="{{route('profile.create')}}" method="POST">@csrf
<ul class="information">
<li>Name :<br>
<input type="text" class="name" name="name">
</li><br>
<li>Gender :<br>
<div class="gender">
<select name="gender" id="" name="gender">
<option class="option" value="" selected="selected">Select Gender</option>
<option value="" >Male</option>
<option value="United States" class="selected">Female</option>
<option value="United Kingdom">Any</option>
</select>
</div>
</li>
<li>Country :<br>
<div class="country">
<select name="country" id="" name="country">
<option value="" selected="selected">Select Country</option>
<option value="United States" class="selected">United States</option>
<option value="United Kingdom">United Kingdom</option>
</select>
</div>
</li><br>
<li>Birthday :<br>
<input type="text" class="birthday" id="bod" name="bod">
</li><br>
<li>User Description :<br>
<textarea name="description" id="" cols="60" rows="10"></textarea></li>
</ul>
<button type="submit" class="saveBtn">Save</button>
</div>
</form>
个人资料表
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('user_id');
$table->string('image');
$table->string('name');
$table->string('gender');
$table->string('country');
$table->string('bod');
$table->string('description');
$table->timestamps();
});
}
UserController.php
class UserController extends Controller
{
public function index() {
return view('profile.index');
}
public function store(Request $request) {
$user_id = auth()->user()->id;
Profile::where('user_id',$user_id)->update([
'name'=>request('name'),
'gender'=>request('gender'),
'country'=>request('country'),
'bod'=>request('bod'),
'description'=>request('description')
]);
return redirect()->route('profile.index');
}
}
答案 0 :(得分:3)
看起来您仅使用两条路线。一种用于索引,一种用于存储。前往创建页面的路线在哪里?添加该路由和控制器方法。
Route::get('user/profile','UserController@index')->name('profile.index');
Route::get('user/profile/create', 'UserController@create')->name('profile.create');
Route::post('user/profile/store', 'UserController@store')->name('profile.store');
控制器
class UserController extends Controller
{
public function index() {
return view('profile.index');
}
public function create() {
return view('profile.create');
}
public function store(Request $request) {
$user_id = auth()->user()->id;
Profile::where('user_id',$user_id)->update([
'name'=>request('name'),
'gender'=>request('gender'),
'country'=>request('country'),
'bod'=>request('bod'),
'description'=>request('description')
]);
return redirect()->route('profile.index');
}
}
表格
<form action="{{route('profile.store')}}" method="POST">
还有您的创建个人资料按钮
<a href="{{ route('profile.crate') }}"><button>Create Profile</button></a>
答案 1 :(得分:0)
请勿将验证和保存结合使用,请单独检查并使用fill()进行保存
UserController.php
$validatedData = $request->validate([
'name' => 'required',
'gender' => 'required',
'country' => 'required',
'bod'=>'required',
'description'=>'required',
]);
Profile::where('user_id',$user_id)->fill([
'name'=>'name',
'gender'=>'gender',
'country'=>'country',
'bod'=>'bod',
'description'=>'description',
])->save();
答案 2 :(得分:0)
我试过了
<块引用>php artisan route:clear
并解决了这个问题。