skip to Main Content

Am displaying user profile data from the database to an input field and at at the same time, the user should be able to update the input field

Controller.php

// shows details in input 
Public function details()
{
    $id = auth()->id;
    $details=Users::where(‘id’,’=‘,$id)->first();

return view (‘profile’,compact(‘details’);
}

// Update function 

Public function details()
{
    // I don’t know what to do
}

blade.php

<form action=‘{{ route(‘update’) }} method=‘post’>
@csrf
<input type=‘text’ name=‘firstname’ value=‘{{ $details->firstname}}’ >

<input type=‘text’ name=‘lastname’ value=‘{{ $details->lastname}}’ >

<input type=‘submit’ value=‘Submit’ >
</form>
    

2

Answers


  1. use IlluminateHttpRequest;

    Public function details(Request $request, User $user)
    {
     
        $user->firstname = $request->firstname;
        $user->lastname = $request->lastname;
        $user->save();
    }
    

    route:

    Route::put('/user/{user}', ...);
    

    blade:

    <form action=‘{{ route(‘update’, $details->id) }} method=‘post’>
    @csrf
    {{method_field('PUT')}}
    <input type=‘text’ name=‘firstname’ value=‘{{ $details->firstname}}’ >
    
    <input type=‘text’ name=‘lastname’ value=‘{{ $details->lastname}}’ >
    
    <input type=‘submit’ value=‘Submit’ >
    </form>
    
    Login or Signup to reply.
  2. I hope you find this useful,

    public function update(Request $request){
    
         $user= User::find($request->id);
    
         $user->firstname = $request->firstname;
    
         $user->lastname = $request->lastname;
    
         $user->save();
    }
    

    Change your blade file:

    <form action="{{ route('user.update') }}" method="post">
    

    Change your web.php file:

    Route::post('user/update','UserController@update')->name('user.update');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search