Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Are you sure you are checking against the correct db?

Try

$user->save(); 
$fetchedUser = User::find($user->id);
var_dump($fetchedUser); 
Last updated 2 years ago.
0

Yes I am sure, as the create() function works flawlessly and to the correct db (checked with phpmyadmin). The index() function of UsersController lists all users in the db, but the save() function does not change them.

BTW: In your suggestion, shouldn't one rather dump $fetchedUser, not $user?

Last updated 2 years ago.
0

TechnicalSkillz said:

BTW: In your suggestion, shouldn't one rather dump $fetchedUser, not $user?

Fixed.

Can you post your model

Last updated 2 years ago.
0

Of course. Here it is:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

	use UserTrait, RemindableTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';
        protected $fillable = ['username','password'];
        

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array('password', 'remember_token');
        public function setPasswordAttribute($pass)
        {
                $this->attributes['password'] = Hash::make($pass);
        }

}
Last updated 2 years ago.
0

That looks fine to me. Could you also post how you are using the save method.

Last updated 2 years ago.
0
    public function update($userID) {
        $validation = Validator::make(Input::all(), ['username' => 'required']);
        if ($validation->fails()) {
            return Redirect::back()->withInput()->withErrors($validation->messages());
        }
        
        $user = User::findOrFail($userID);
        
        $user->username = Input::get('username');
        if(Input::has('password'))
        {
            $user->password = Input::get('password');
        }
        
        $user->save();


        return Redirect::route('acp.users.index')->with('success', 'created');
    }
Last updated 2 years ago.
0

I found the error myself. The problem was that I named the ID column in the users table as "ID", not as "id". Apparently, MySQL didn't have a problem with that (case-insensitive?), thus returning the correct user, but when Laravel tried to update the user, it tried to retrieve the ID from the field $user->id, and PHP, being case-sensivtive, returned null. Obviously, updating the user with the ID "null" could not work.

Last updated 2 years ago.
0

I had similar problem. The cause was that the code was inside try {} catch {} block. Eloquent thows PDOExeption but I forgot to handle it. So my logs didn't notice me about an Exception nor I din't catch it :P

0

Thanks, guys, you saved my day.

I too, had the "ID" column in capitals, because some code samples floating around in the Net suggest so.

The question still remains, however, why Laravel/Eloquent does not complain, but silently drop the save() instead.

Thanks though, my code works now,

Armin.

0

I just ran into the same problem. In my case, I had a method that always called ->restore() on a model without checking if it was actually trashed. It turns out that calling restore() on a not-yet-saved model causes it to be attempted saved.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.