app/Http/Controllers/Empresa/UsuarioController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $data  = $request->except(['foto', 'password']);
        $email = User::where('email', $data['email'])->withTrashed()->get();
        if ($email->count() > 0) {
            return back()->withErrors(['email'=>'Email ya existe'])->withInput();
        }
        if ($request->has('password')) {
            if (strlen($request->get('password')) > 5) {
                $data['password'] = bcrypt($request->get('password'));
            } else {
                return back()->withErrors(['password'=>'Contraseña demasiado corta, minimo 6 caracteres'])->withInput();
            }
        }
        $usuario = User::create($data);
        if ($request->has('foto')) {
            $usuario->foto = $request->file('foto')->store('public/usuarios');
            $usuario->save();
        }
        $usuario->syncRoles($request->get('role'));
        if ($usuario->empresa_id == 0) {
            $usuario->empresa_id = Auth::user()->empresa_id;
            $usuario->save();
        }
        $usuario->notify(new NuevoUsuarioNotification($usuario, $request->get('password')));
        if ($request->is('api/*')) {
            $vendedores = User::where('empresa_id', auth('api')->user()->empresa_id)->with(['roles'])->orderBy('nombre')->get();
            return response()->json(compact('vendedores'));
        }
        if (Auth::user()->hasRole('SuperAdministrador')) {
            return redirect('empresa/' . $usuario->empresa_id);
        }
        return redirect('e/usuario');
    }
public function store ( Request $ request ) { $ data = $ request -> except ( [ 'foto' , 'password' ] ) ; $ email = User :: where ( 'email' , $ data [ 'email' ] ) -> withTrashed ( ) -> get ( ) ; if ( $ email -> count ( ) > 0 ) { return back ( ) -> withErrors ( [ 'email' => 'Email ya existe' ] ) -> withInput ( ) ; } if ( $ request -> has ( 'password' ) ) { if ( strlen ( $ request -> get ( 'password' ) ) > 5 ) { $ data [ 'password' ] = bcrypt ( $ request -> get ( 'password' ) ) ; } else { return back ( ) -> withErrors ( [ 'password' => 'Contraseña demasiado corta, minimo 6 caracteres'] ) - >w ithInput( ) ; } } $ usuario = User :: create ( $ data ) ; if ( $ request -> has ( 'foto' ) ) { $ usuario -> foto = $ request -> file ( 'foto' ) -> store ( 'public/usuarios' ) ; $ usuario -> save ( ) ; } $ usuario -> syncRoles ( $ request -> get ( 'role' ) ) ; if ( $ usuario -> empresa_id == 0 ) { $ usuario -> empresa_id = Auth :: user ( ) -> empresa_id ; $ usuario -> save ( ) ; } $ usuario -> notify ( new NuevoUsuarioNotification ( $ usuario , $ request -> get ( 'password' ) ) ) ; if ( $ request -> is ( 'api/*' ) ) { $ vendedores = User :: where ( 'empresa_id' , auth ( 'api' ) -> user ( ) -> empresa_id ) -> with ( [ 'roles' ] ) -> orderBy ( 'nombre' ) -> get ( ) ; return response ( ) -> json ( compact ( 'vendedores' ) ) ; } if ( Auth :: user ( ) -> hasRole ( 'SuperAdministrador' ) ) { return redirect ( 'empresa/' . $ usuario -> empresa_id ) ; } return redirect ( 'e/usuario' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .