mercredi 30 mai 2018

Laravel 5.6 - eloquent create won't accept array

For some reason the create method won't accept the passed array when I try to create new user.

I am getting following error:

SQLSTATE[HY000]: General error: 1364 Field 'first_name' doesn't have a default value (SQL: insert into `users` (`updated_at`, `created_at`) values (2018-05-30 13:24:33, 2018-05-30 13:24:33))

This is the current code:

public function storeUser($data, $request)
    {
        if($request->hasFile('avatar'))
            $data['avatar'] = $this->uploadAvatar($request->file('avatar'));

        $password = \Hash::make($data['temp_password']);

        $data['password']           = $password;
        $data['temporary_password'] = $password;
        $data['social']             = Strings::arrayToJson($data['social']);

        // Assign the clinic_id from the select menu (only super admin have the permission to do this)
        if(\Auth::user()->hasRole('super_admin') && isset($data['clinic_user']))
            $data['clinic_id'] = (int) $data['clinic_user'];

        // If the admin of the clinic is creating the user we wiil use his clinic_id for the new user
        if(\Auth::user()->hasRole('admin'))
            $data['clinic_id'] = \Auth::user()->clinic_id;

        if($this->create($data)){

            event(new NewUser($this, $data['temp_password']));

            if(\Auth::user()->hasRole('super_admin') && isset($data['clinic_owner']))
                event(new ClinicUpdate($this));

            if(\Auth::user()->hasRole('super_admin', 'admin') && \Auth::user()->id !== $this->id)
                $user->giveRole($data['user_role']);

            return true;
        }

        return false;

    }

Fillable:

protected $fillable = [
        'first_name', 'last_name', 'about', 'education', 'position', 'phone',
        'social', 'title', 'gender', 'avatar', 'location', 'email', 'password',
        'temporary_password', 'verified', 'clinic_id'
    ];

This the dd for the data:

array:17 [▼
  "user_role" => "admin"
  "clinic_user" => "1"
  "first_name" => "Sasha"
  "last_name" => "Miljkovic"
  "email" => "xxxxxx@xxxxx.com"
  "temp_password" => "12345678"
  "title" => "10"
  "gender" => "0"
  "position" => "Tester"
  "phone" => "1234"
  "location" => "Pirot"
  "about" => "Maecenas gravida tellus augue, sed mollis quam viverra at. Aenean sit amet dui non eros laoreet porta et nec nisi. Cras lectus justo, porttitor quis mattis nec, ▶"
  "social" => "[]"
  "avatar" => "avatar_5b0ea46d2af8d.jpg"
  "password" => "$2y$10$gxGPBbwS44KHXLn57leRpukX/zu/rX3SSn7jRdM27kvQb9N84CcGa"
  "temporary_password" => "$2y$10$gxGPBbwS44KHXLn57leRpukX/zu/rX3SSn7jRdM27kvQb9N84CcGa"
  "clinic_id" => 1
]



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire