Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create new user in Laravel?

I created the model:

<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class ClientModel extends Eloquent implements UserInterface, RemindableInterface {

    protected $connection = 'local_db';
    protected $table      = 'administrators';
    protected $fillable   = ['user_id'];

    public function getAuthIdentifier()
    {
        return $this->username;
    }

    public function getAuthPassword()
    {
        return $this->password;
    }

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    public function getReminderEmail()
    {
        return $this->email;
    }
}

When I try to use it like this:

ClientModel::create(array(
    'username' => 'first_user',
    'password' => Hash::make('123456'),
    'email'    => '[email protected]'
));

It creates empty entry in DB...

enter image description here

like image 674
user1692333 Avatar asked Sep 22 '14 09:09

user1692333


2 Answers

Nowadays way :

User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

or even:

        $arrLcl = [];
        $arrLcl['name'] = $data['name'];
        $arrLcl['email'] = $data['email'];
        $arrLcl['password'] = $data['password'];
        User::create($arrLcl);
like image 192
CodeToLife Avatar answered Sep 28 '22 20:09

CodeToLife


I think you make it too complicated. There is no need to make it this way. By default you have User model created and you should be able simple to create user this way:

$user = new User();
$user->username = 'something';
$user->password = Hash::make('userpassword');
$user->email = '[email protected]';
$user->save();

Maybe you wanted to achieve something more but I don't understand what you use so many methods here if you don't modify input or output here.

like image 39
Marcin Nabiałek Avatar answered Sep 28 '22 19:09

Marcin Nabiałek