0

i'm trying to create a new user using the following code:

$user = new User();
$arr = array(
    "username" => Input::get('username'),
    "pass" => Input::get('pass')

)
$user->save($arr);

it does add the record to the table but all fields are empty - any idea what's wrong? thanks

Fuxi
  • 7,611
  • 25
  • 93
  • 139

2 Answers2

0

That's not how the save function works! You should take another look at the Laravel docs on Eloquent.

The User::create() static method, the class constructor and the update method all do take arrays as input, but the save method does not.

You could do:

$arr = array(
    "username" => Input::get('username'),
    "pass" => Input::get('pass')
)

$user = new User($arr);
$user->save();

Making sure that the username and pass columns are mass-assignable (exist in the model's protected $fillable array).

You aren't going to store the user password in plain text, right?

0

If you use:

$user = new User([
    "username" => Input::get('username'),
    "pass" => Input::get('pass')
]);
$user->save();

or

User::create([
    "username" => Input::get('username'),
    "pass" => Input::get('pass')
]);

The username and pass fields must be mass-assignable, http://laravel.com/docs/5.1/eloquent#mass-assignment

Otherwise, you should do the following:

$user = new User;
$user->username = Input::get('username');
$user->pass = bcrypt(Input::get('pass'));
$user->save();
user2094178
  • 9,204
  • 10
  • 41
  • 70