lundi 4 avril 2016

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');

});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire