0

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.

5
  • @Aldible - Can you please just run dd($input) after the final array_add and see if password actually exists? Commented Oct 26, 2015 at 9:47
  • That is not how the array_add function in laravel works. Check the manual: laravel.com/docs/5.1/helpers#method-array-add Commented Oct 26, 2015 at 9:48
  • @Phorce added the dd($input) result Commented Oct 26, 2015 at 9:51
  • @Aldibe Instead of: $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? Commented Oct 26, 2015 at 9:57
  • @Phorce nope, but the error's getting strange. So i did $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") Commented Oct 26, 2015 at 10:04

1 Answer 1

1

I prefer to add key-value pairs in PHP this way:

//adding new items to request array
$input = $request->all();
$input['created_by_user_id'] = Auth::user()->user_id;
$input['password'] = $passcrypt;

ensure that protected $fillable contains all required keys before create

use constants for switch:

const CREATE = 'create';

public function changeFillableMode($mode){
        switch($mode){
            case self::CREATE:
            $this->fillable = ['fullname', 'email', 'password', 'description','permission_id','created_by_user_id','has_change_password'];
            break;
        }
    }

and call it:

$user->changeFillableMode(User::CREATE);
Sign up to request clarification or add additional context in comments.

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.