Are you sure you are checking against the correct db?
Try
$user->save();
$fetchedUser = User::find($user->id);
var_dump($fetchedUser);
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?
TechnicalSkillz said:
BTW: In your suggestion, shouldn't one rather dump $fetchedUser, not $user?
Fixed.
Can you post your model
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);
}
}
That looks fine to me. Could you also post how you are using the save method.
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');
}
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.
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
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.
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community