I'm creating an Add-User function in my website (Laravel 5.1), in which an admin account can create an account using an email, and the password will be sent to that email. The form in the Add-User view asks for the name, email, permission level, and description. The create function supposed to accept a request from that wiew, generate a password, add the password and the creator's ID into the request, create a DB entry based on the request, then send an email to that address. I'm currently stuck at creating the DB based on the request. Here is my function :
public function create(Request $request)
{
//generate password
$pass = str_random(10);
$passcrypt = bcrypt($pass);
$email = $request->input('email');
//change the fillable in the model
$user = new User;
$user->changeFillableMode("CREATE");
//validation
$this->validate($request,['fullname'=>'required',
'email'=>'required|email',
'permission_id'=>'required']);
//adding new items to request array
$input = $request->all();
$input = array_add($input,'created_by_user_id',Auth::user()->user_id);
$input = array_add($input,'password',$passcrypt);
$user->create($input);
//send mail
$data['email'] = $request->input('email');
$data['pass'] = $pass;
Mail::queue('mail.mailbodycreate', $data, function($message) use ($email)
{
$message->to($email)->subject('Account info');
});
}
The $input already shows that the password and creator Id are already in the array, but I keep getting error that it's not in the array (since password is not nullable in my migration). can anyone help?
update : I add dd($input); after the last array_add. this is the result
array:7 [
"_token" => "9VowN9ICgkAb9cegbbQzhFtfIhmQr0DqlGj724bN"
"fullname" => "Aldi"
"email" => "[email protected]"
"permission_id" => "1"
"description" => "testing add user"
"created_by_user_id" => 4
"password" => "$2y$10$Dc4TZqMYE1kyPc7wFHBT1.8KUzk35QqV32wKegstjMFHnD/rhjsw6"
]
update 2 : here is the model for the User table :
protected $table = 'msuser';
protected $primaryKey = 'user_id';
protected $fillable = ['fullname', 'email', 'description','permission_id'];
protected $hidden = ['password', 'remember_token'];
protected $dates = ['deleted_at'];
public function changeFillableMode($mode){
switch($mode){
case "CREATE" :
$this->fillable = ['fullname', 'email', 'password', 'description','permission_id','created_by_user_id','has_change_password'];
break;
}
}
the changeFillableMode is used to change the content of $fillable in the controller function.
dd($input)after the finalarray_addand see if password actually exists?$user->create($input);can you just do:$user = new User; $user->fullName = "test"; $user->email="[email protected]" ... Fill in all of the elements, and then just run$user->create()` does that give you the same reply?$user->db_column = $input['db_column'];for all of the elements, now i got :DETAIL: Failing row contains (111, null, null, null, null, null, f, null, null, 2015-10-26 09:58:01, 2015-10-26 09:58:01, null). (SQL: insert into "msuser" ("updated_at", "created_at") values (2015-10-26 09:58:01, 2015-10-26 09:58:01) returning "user_id")