Aldibe
Aldibe

Reputation: 1347

Laravel create not accepting all input array

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.

Upvotes: 0

Views: 1349

Answers (1)

Jarik Voroniak
Jarik Voroniak

Reputation: 29

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

Upvotes: 1

Related Questions