0

I am currently running into an issue with my update function in my UserController. The only field that will update correctly is the eid field, all other fields just cause the page to refresh.

I have looked at several other update function examples and I can't determine what

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>

Migration File

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 results

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

dd after validation

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 after save

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"
}
Mathieu Ferre
  • 4,246
  • 1
  • 14
  • 31
darkdelusions
  • 183
  • 1
  • 2
  • 11
  • your user model should have protected $fillable = ['name', 'email', 'eid', 'password']; – Leo Dec 21 '17 at 15:45
  • 1
    @LeoinstanceofKelmendi no, it shouldn't because OP doesn't use mass assignment. – Alexey Mezenin Dec 21 '17 at 15:48
  • Leo, I am pulling in App\User at the top of the file which has my $fillable in it protected $fillable = [ 'name', 'eid', 'email', 'password', ]; – darkdelusions Dec 21 '17 at 15:48
  • @darkdelusions please post results of `dd($request->all());` and migration file for `users` table. – Alexey Mezenin Dec 21 '17 at 15:49
  • @darkdelusions right I see your issue add type = "submit" – Leo Dec 21 '17 at 15:55
  • But OP says `eid` is updating normally, this means the form is working. Maybe he's using JS or something to submit the form. – Alexey Mezenin Dec 21 '17 at 15:58
  • well if he is using js then we need that info! @AlexeyMezenin – Leo Dec 21 '17 at 15:59
  • @AlexeyMezenin results above for formatting reasons – darkdelusions Dec 21 '17 at 16:08
  • @darkdelusions the request array looks fine too. What about the migration file? – Alexey Mezenin Dec 21 '17 at 16:11
  • @AlexeyMezenin posted it above as well here is the github for my code base https://github.com/darkdelusions/support – darkdelusions Dec 21 '17 at 16:13
  • The code looks fine except the submit button. Maybe you're using some kind of cache (OPcache or something)? Also, try to [clear all Laravel cache](https://stackoverflow.com/questions/37259103/laravel-application-is-not-working-after-uploading-to-the-server/37259162#37259162). – Alexey Mezenin Dec 21 '17 at 16:29
  • @AlexeyMezenin corrected the submit button when leo suggested it, and just cleared all the cache and no go and I have no cache plugins – darkdelusions Dec 21 '17 at 16:35
  • @darkdelusions well, it's super weird. Put `dd(User::find($id));` right after validation (before the `$user = User::...`) and please post results. Then put the same `dd(User::find($id));` right after `$user->save();` and post results for this too and also please update `dd($request->all());` results for the same request. But when you'll do that, do not fill the form again and again, just change the code and press refresh button in browser to make sure the data is the same for all three `dd()`. – Alexey Mezenin Dec 21 '17 at 16:42
  • I've found that confirming changes can be done with a `$originalAttributes = $model->getAttributes(); ... $model->save(); ... $newAttributes = $model->getAttributes(); ... dd($originalAttributes, $newAttributes);`; should show the values before and after a save in the same `dd()` statement. – Tim Lewis Dec 21 '17 at 16:59
  • 1
    Try `dd($errors->all())` in your view to see any validation errors. – Martin Bean Dec 21 '17 at 17:04
  • 1
    @MartinBean but OP said `eid` was updated and other fields weren't. – Alexey Mezenin Dec 21 '17 at 17:07
  • @darkdelusions I've looked into the `dd` results and I don't see `eid` was updated as you've said before. Also, you're using the same name as it was before. – Alexey Mezenin Dec 21 '17 at 17:13
  • @MartinBean that solved the issue, when i added the dd($errors->all()) to the view it returned an error because i had eid set to unique:users preventing the form from posting This can be marked solved. – darkdelusions Dec 21 '17 at 17:16
  • @AlexeyMezenin thank you for all your help as well – darkdelusions Dec 21 '17 at 17:16

0 Answers0