user6060368
user6060368

Reputation:

Data is not updating in database on update Laravel 5.2

I just implement the CRUD in my Application , but UPDATE is not updating data in Database I var_dump($user->name) but it outputs nothing . Please take a look on the code and help me out.I am using Laravel 5.2 and Resource Controller.

PS : Edit is working fine e.g on this URL http://localhost/pos/users/6/edit?id=6 I can populate the view fields with the required data but when I hit update nothing updates in Database.

UserController:

  public function edit($id)
        {
            $user = User::find($id);
            return view('user.update')->with('userToUpdate',$user);
        }

        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $id)
        {
            $user = User::find($id);
            $user->name = Input::get('name');
            $user->email = Input::get('email');
            $user->password = bcrypt(Input::get('password'));
            $user->save();      
            return Redirect::to('/users')->with('message', 'User Updated');
        }

UpdateView:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Update</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="PUT" action="{{ url('/users') }}">
                        {!! csrf_field() !!}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">Name</label>

                            <div class="col-md-6">
                                <input type="text" class="form-control" name="name" value="{!! $userToUpdate->name !!}">

                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input type="email" class="form-control" name="email" value="{!! $userToUpdate->email !!}">
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password" value="{!! $userToUpdate->password !!}">
                            </div>
                        </div>


                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-btn fa-user"></i>Update
                                </button>
                            </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

UserModel:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Routes:

Route::group(['middleware' => 'web'], function () {
    Route::get('/', function () {
        return view('welcome');
    });

    Route::auth();

    Route::get('/home', 'HomeController@index');
    Route::get('/register', function(){
        return view('auth.register');
    })->middleware('isAdmin');
    Route::resource('/users', 'UsersController');

});

Upvotes: 1

Views: 4886

Answers (2)

apokryfos
apokryfos

Reputation: 40730

From the laravel docs:

https://laravel.com/docs/5.0/routing#method-spoofing

HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form.

However the Route::resource specifies that the update needs to use the PUT method. Therefore your form needs to be:

<form class="form-horizontal" role="form" method="POST" action="{{ url('/users', $userToUpdate->id) }}">
     {!! csrf_field() !!}
     <input type="hidden" name="_method" value="PUT">

Upvotes: 4

Harry Loyd
Harry Loyd

Reputation: 429

Is it because your not passing the user id when sending off the form? Try editing the form action from {{ url('/users') }} to {{ url('/users', $userToUpdate->id) }}.

Upvotes: 3

Related Questions