简体   繁体   English

更新Laravel 5.2时数据库中的数据未更新

[英]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 . 我只是在应用程序中实现CRUD ,但UPDATE不会更新数据库var_dump($user->name)但不会输出任何内容。 Please take a look on the code and help me out.I am using Laravel 5.2 and Resource Controller. 请看一下代码并帮帮我。我正在使用Laravel 5.2和Resource Controller。

PS : Edit is working fine eg 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. PS:编辑工作正常,例如在此URL上http://localhost/pos/users/6/edit?id=6我可以用所需的数据填充视图字段,但是当我单击update时,数据库中没有任何更新。

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

});

Is it because your not passing the user id when sending off the form? 是否因为发送表单时未传递用户ID? Try editing the form action from {{ url('/users') }} to {{ url('/users', $userToUpdate->id) }} . 尝试将表单操作从{{ url('/users') }}{{ url('/users', $userToUpdate->id) }}

From the laravel docs: 从laravel文档中:

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

HTML forms do not support PUT, PATCH or DELETE actions. HTML表单不支持PUT,PATCH或DELETE操作。 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. 因此,在定义从HTML表单调用的PUT,PATCH或DELETE路由时,您将需要向表单添加一个隐藏的_method字段。

However the Route::resource specifies that the update needs to use the PUT method. 但是Route :: resource指定更新需要使用PUT方法。 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">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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