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

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);
}
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

The values are all set after doing that..including the _token in the array.

Last updated 2 years ago.
0

Strange... it should work.

Have you tried User::create(Input::all());?

Can you show the migration file. Just up() method. And the form

Last updated 2 years ago.
0

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;
}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ooiginla ooiginla Joined 22 Aug 2014

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.