AliReza Eshaqi
AliReza Eshaqi

Reputation: 11

Update the user

I want to update the user using this Form:

        {!! Form::model($user ,['method'=>'PATCH', 'action'=>['AdminController@update',$user->id], 'files'=>true]) !!}

        <div class="form-group">
            {!! Form::label('first_name','First Name:') !!}
            {!! Form::text('first_name',null,['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('last_name','Last Name:') !!}
            {!! Form::text('last_name',null,['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('username','Username (Student ID):') !!}
            {!! Form::number('username',null,['class'=>'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::label('email','Email:') !!}
            {!! Form::email('email',null,['class'=>'form-control']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('address','Address:') !!}
            {!! Form::text('address','null',['class'=>'form-control']) !!}
         </div>

         <div class="form-group">
            {!! Form::label('phone_number','Phone Number:') !!}
            {!! Form::number('phone_number','null',['class'=>'form-control']) !!}
         </div>



        <div class="form-group">
            {!! Form::label('password','Password:') !!}
            {!! Form::password('password',['class'=>'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::submit('Update User', ['class'=>'btn btn-primary col-sm-6']) !!}
        </div>

        {!! Form::close() !!}

and this is my Route:

Route::prefix('admin')->group(function(){
  Route::post('/users/{id}','AdminController@update');
)};

when I press update button I receive this error:

error message that I received

Please help me, I am Using Laravel 5.4

Upvotes: 0

Views: 57

Answers (4)

Rajinder
Rajinder

Reputation: 1091

try following

{{ Form::model($user, array('route' => array('users.update', $user->id), 'method' => 'PUT'))  }}

 // form fiedls come here    

{!! Form::close() !!}

in route file add following route:

Route::resource('users', "UserController");

and in user controller add following code:

 public function update(Request $request, $id){

    // Add form validation here


    $user = User::find($id);
    $user->first_name = $request->first_name;
    $user->last_name = $request->last_name;
    $user->save();

    Session::Flash('success', 'The User have been updated successfully!');
    return redirect()->route('users.index');

} 

don't user AdminController to manage users.

Upvotes: 1

Abdi
Abdi

Reputation: 608

you can use this code to fix every thing

form blade

{{ Form::model($user, array('route' => array('update-user', $user->id), 'method' => 'PUT'))  }}

route

Route::prefix('admin')->group(function(){
    Route::PUT('/users/{id}','AdminController@update')->name('update-user');
 )};

Upvotes: 0

PHP Geek
PHP Geek

Reputation: 4033

Please use PATCH in routes for example :

Route::patch('/users/{id}','AdminController@update');

Upvotes: 0

Abdi
Abdi

Reputation: 608

your method is post , but you send patch request

you should change to this

'method'=>'post'

Upvotes: 0

Related Questions