简体   繁体   中英

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? Can you help me? I tried the @method('PUT') method before. 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

...

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 . I will suggest you to use POST method so that user_id will not be append in URL , so try this,

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

 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 }}"

to

action="{{ url('edit-name',$user->id) }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM