[英]Laravel 9 Update Form Values with controller MVC
I want to update my input values in the form in my database by sending them from the route to the controller. What am I doing wrong?我想通过将它们从路由发送到 controller 来更新我数据库中表单中的输入值。我做错了什么? Can you help me?
你能帮助我吗? I tried the @method('PUT') method before.
我之前尝试过 @method('PUT') 方法。 It's a bit confusing.
这有点令人困惑。 I am sharing the codes.
我正在分享代码。 I will be very happy if you can help, thank you.
如果您能提供帮助,我将非常高兴,谢谢。
... ...
<div class="modal-body">
<div class="form-group text-center"
style="position: center; margin-top:3em;">
<form action="{{ 'edit-name/' . $user->id }}" method="POST"
enctype="multipart/form-data">
@csrf
<label for="fname">
{{ __('welcome.fullname') }}:</label>
<input name="name1" type="hidden"
placeholder="{{ $user->name }}">
</div>
<br>
<br>
<br>
{{-- //Name Edit Button --}}
<button type="submit"
class="btn-sm app-btn-secondary">{{ __('welcome.confirm') }}</button>
... ...
my route and controller我的路线和 controller
... ...
Route::get('edit-name/{id}', [HomeController::class, 'editname']);
... ...
... ...
public function editname(Request $request, $id){
dd(1);
$user= Auth::user();
dd($user);
$id=$request->id;
$name=$request->input('name1');
$isUpdateSuccess = User::where('id', $id) ->update(['name1'=>$name, ]);
if($isUpdateSuccess)
echo '<h1>Update Success</h1>';
else echo '<h1>Update Failed </h1>';
}
... ...
you are sending the data from modal using POST method but in your route you are using this data by GET method .您正在使用POST 方法从模态发送数据,但在您的路线中您正在通过GET 方法使用此数据。 I will suggest you to use POST method so that user_id will not be append in URL , so try this,
我会建议你使用POST方法,这样 user_id 就不会是 URL 中的append ,所以试试这个,
Your blade你的刀片
<div class="modal-body">
<div class="form-group text-center" style="position: center; margin-top:3em;">
<form action="{{ 'edit-name' }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="hidden" name="id" placeholder="{{ $user->id }}">
<label for="fname">
{{ __('welcome.fullname') }}:</label>
<input name="name1" type="text" placeholder="{{ $user->name }}">
</div>
<br>
<br>
<br>
{{-- //Name Edit Button --}}
<button type="submit" class="btn-sm app-btn-secondary">{{__('welcome.confirm') }}</button>
Your route你的路线
Route::post('/edit-name', [HomeController::class, 'editname']);
Your controller你的 controller
public function editname(Request $request){
// dd(1);
$user= Auth::user();
// dd($user);
$id=$request->id;
$name=$request->input('name1');
$isUpdateSuccess = User::where('id', $id) ->update(['name1'=>$name, ]);
if($isUpdateSuccess)
echo '<h1>Update Success</h1>';
else echo '<h1>Update Failed </h1>';
}
Change action改变行动
from从
action="{{ 'edit-name/'. $user->id }}"
action="{{ 'edit-name/'. $user->id }}"
to到
action="{{ url('edit-name',$user->id) }}"
action="{{ url('edit-name',$user->id) }}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.