16

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

3 Answers 3

27

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.

Sign up to request clarification or add additional context in comments.

Comments

9

You are using create method (Mass Assignment) so it's not working because you have this:

// Only user_id is allowed to insert by create method
protected $fillable = ['user_id'];

Put this in your model instead of $fillable:

// Allow any field to be inserted
protected $guarded = [];

Also you may use the alternative:

protected $fillable = ['username', 'password', 'email'];

Read more about Mass Assignment on Laravel website. While this may solve the issue but be aware of it. You may use this approach instead:

$user = new User;
$user->username = 'jhondoe';
// Set other fields ...
$user->save();

Comments

8

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.