You can do something like
$user = new User;
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'); // never save plain text passwords
$user->save();
Another way would be to add
protected $fillable = array('username', 'password');
in your User Model, and in controller:
$input = Input::all();
// or select only required fields
$input = Input::only('username', 'password');
$user = User::create($input);
Edit
Second version would save password in plain text, so add this in User model. Probably works for first example too
public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::make($password);
}
Thanks a lot for the advice on password hashing, and $fillables. but after applying and when i call the $obj->save(), a new row is inserted to my database but fills in null to all the fields.
Are form input names the same as DB fields?
Maybe try in controller method
dd(Input::all()); // die and dump values
if the values are even set.
The values are all set after doing that..including the _token in the array.
Strange... it should work.
Have you tried User::create(Input::all());?
Can you show the migration file. Just up() method. And the form
Thanks a lot, the User::Create() works like charm. i guess my eyes omitted the line tutorials which says you can think of the class attributes of these objects(ORM) as the individual columns for the table that i read from daylerees website. i was busy creating each columns of the database as class attributes as shown below until i commented it out. I don't know its set by default, so i guess it was overriding and since it was not initialized, null values were stored in the database. Thanks a lot. Now the obj->save() works.
Class User extends Eloquent{
// each table columns below
public $username;
public $password;
public $otherfields;
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community