Home > OS >  Laravel - Random password on create
Laravel - Random password on create

Time:11-30

I have a method that stores employee data on create however I have defined a default password to be randomly created and hashed. The password isn't stored for some reason.

Any ideas?

public function store(Request $request)
    {
        // get company
        $company = Auth::user()->companies()->first();

        // get and validate data
        $storeData = $request->validate([
            'firstname' => 'required',
            'lastname' => 'required',
            'email' => 'required|email',
            'phone' => 'required|numeric|digits:11'
        ]);

        // create employee with validated data
        $employee = $company->employees()->create(array_merge($storeData), [
            'password' => Hash::make(Str::random(40)),
        ]);

        return redirect('/employees/' . $employee->id )
            ->with('success', 'Employee successfully created');
    }

CodePudding user response:

There's an typo in your code, try

   $employee = $company->employees()->create(array_merge($storeData, [
            'password' => Hash::make(Str::random(40)),
   ]));

The array_merge is closed too early and only contains what you have in the $storeData array.

  • Related