我创建了模型:
<?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;
}
}
当我尝试使用它时:
ClientModel::create(array(
'username' => 'first_user',
'password' => Hash::make('123456'),
'email' => 'my@email.com'
));
它在DB中创建空条目......
答案 0 :(得分:5)
我认为你太复杂了。没有必要这样做。默认情况下,您创建了User
模型,您应该能够以这种方式创建用户:
$user = new User();
$user->username = 'something';
$user->password = Hash::make('userpassword');
$user->email = 'useremail@something.com';
$user->save();
也许你想要实现更多目标,但如果你不在这里修改输入或输出,我不明白你在这里使用了多少方法。
答案 1 :(得分:3)
现在的方式:
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
甚至:
$arrLcl = [];
$arrLcl['name'] = $data['name'];
$arrLcl['email'] = $data['email'];
$arrLcl['password'] = $data['password'];
User::create($arrLcl);
答案 2 :(得分:2)
您正在使用create
方法(质量分配),因此您无法使用此方法:
// Only user_id is allowed to insert by create method
protected $fillable = ['user_id'];
将其放入您的模型而不是$fillable
:
// Allow any field to be inserted
protected $guarded = [];
您也可以使用替代方案:
protected $fillable = ['username', 'password', 'email'];
详细了解Laravel
网站上的Mass Assignment。虽然这可以解决问题,但要注意它。您可以改用此方法:
$user = new User;
$user->username = 'jhondoe';
// Set other fields ...
$user->save();