Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel – Random password on create

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading