/** * Create a new user with data sent from the create form * @param CreateUserRequest $request we validate the data sent from the form in this class before we continue our logic * @return \Illuminate\Support\Facades\Redirect redirect the user to the users index view */ public function store(CreateUserRequest $request, User $UserModel) { // fill the model with the sent from the form $UserModel->fill($request->all()); // add the password after encrypting it $UserModel->password = bcrypt($request->input('password')); // save the user $UserModel->save(); // sync roles with user if ($request->has('role')) { foreach ($request->input('role') as $role) { $UserModel->attachRole($role); } } // we process the user permissions based on the new ones sent from the view $this->processPermissions($UserModel); // the message that will be sent back to the user $message = trans('users::users.create_success', ['name' => $UserModel->name]); // redirect back to the users index if (request('submit') == 'save') { return redirect()->back()->with('success', $message); } return redirect()->route('users.index')->with('success', $message); }