更新功能是UsersController,只保存一个字段

时间:2017-12-21 15:42:11

标签: laravel

我目前在UserController中遇到了更新功能的问题。唯一正确更新的字段是eid字段,所有其他字段只会导致页面刷新。

我已经查看了其他几个更新函数示例,我无法确定

public function update(Request $request, $id)
{
    $this->validate($request,[
        'name' => 'required|max:255',
        'eid' => 'required|max:255|unique:users',
        'email' => 'required|email|unique:users,email,'.$id,
    ]);

    $user = User::findOrFail($id);
    $user->name = $request->name;
    $user->eid = $request->eid;
    $user->email = $request->email;
     if (!empty($request->password)) {
        $user->password = Hash::make($request->password);
    }
    $user->save();

    return redirect()->route('users.show', $id);

edit.blade.php

<form action="{{route('users.update', $user->id)}}" method="POST">
            {{csrf_field()}}
            {{method_field('PATCH')}}
            <div class="field">
                <label for="name" class="label">Name</label>
                <p class="control">
                    <input type="text" class="input" name="name" id="name" value="{{$user->name}}">
                </p>
            </div>
            <div class="field">
                <label for="eid" class="label">EID</label>
                <p class="control">
                    <input type="text" class="input" name="eid" id="eid" value="{{$user->eid}}">
                </p>
            </div>
            <div class="field">
                <label for="email" class="label">Email</label>
                <p class="control">
                    <input type="text" class="input" name="email" id="email" value="{{$user->email}}">
                </p>
            </div>
            <div class="field">
                <label for="password" class="label">Password</label>

                <p class="control m-t-10">

                    <input type="text" class="input" name="password" id="password" placeholder="Enter your password">
                </p>
            </div>
            <button class="button is-success is-pulled-right" type="submit">Update User</button>
        </form>

迁移文件

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('eid');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

dd结果

array:5 [▼
"_token" => "uG3qvyPAa2OBGFUZ7PNwNRKhupNyQLeptxHsSi8a"
"_method" => "PATCH"
"name" => "Agent11"
"eid" => "RQrOwau11"
 "email" => "agent12@app.com"
]
验证后

dd

User {#271 ▼
#fillable: array:4 [▼
0 => "name"
1 => "eid"
2 => "email"
3 => "password"
]
#hidden: array:2 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [▼
"id" => 6
"name" => "Agent11"
"eid" => "RQrOwau"
"email" => "agent@app.com"
"password" => "$2y$10$KC3UVyoLPhtb/omRT8gxjuHe/QB8t5Lj/JYLugbHMfQ9O4NyduV5a"
"remember_token" => null
"created_at" => "2017-12-19 15:21:23"
"updated_at" => "2017-12-21 10:33:40"
]
#original: array:8 [▼
"id" => 6
"name" => "Agent11"
"eid" => "RQrOwau"
"email" => "agent@app.com"
"password" => "$2y$10$KC3UVyoLPhtb/omRT8gxjuHe/QB8t5Lj/JYLugbHMfQ9O4NyduV5a"
"remember_token" => null
"created_at" => "2017-12-19 15:21:23"
"updated_at" => "2017-12-21 10:33:40"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
保存后

dd

User {#271 ▼
#fillable: array:4 [▼
0 => "name"
1 => "eid"
2 => "email"
3 => "password"
]
#hidden: array:2 [▼
0 => "password"
1 => "remember_token"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
 #keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [▼
"id" => 6
"name" => "Agent11"
"eid" => "RQrOwau"
"email" => "agent@app.com"
"password" => "$2y$10$KC3UVyoLPhtb/omRT8gxjuHe/QB8t5Lj/JYLugbHMfQ9O4NyduV5a"
"remember_token" => null
"created_at" => "2017-12-19 15:21:23"
"updated_at" => "2017-12-21 10:50:46"
]
#original: array:8 [▼
"id" => 6
"name" => "Agent11"
"eid" => "RQrOwau"
"email" => "agent@app.com"
"password" => "$2y$10$KC3UVyoLPhtb/omRT8gxjuHe/QB8t5Lj/JYLugbHMfQ9O4NyduV5a"
"remember_token" => null
"created_at" => "2017-12-19 15:21:23"
"updated_at" => "2017-12-21 10:50:46"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}

0 个答案:

没有答案