realty/app/Http/Controllers/API/v1/UserController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int|string  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        if ($id === 'self') {
            $user = auth()->user();
        } else {
            $user = User::findOrFail($id);
        }
        $this->authorize('update', $user);
        $rules = [
            'image_id' => 'nullable|integer|exists:images,id',
            'f_name'   => 'string|max:191',
            'm_name'   => 'nullable|string|max:191',
            'l_name'   => 'string|max:191',
            'email' => [
                'string',
                'email',
                'max:191',
                Rule::unique('users')->ignore($user),
            ],
        ];
        if ($user->id == auth()->id()) {
            $rules['cur_password'] = 'required_with:new_password|password';
            $rules['new_password'] = 'string|min:8|confirmed';
        } else {
            $rules['role']     = 'string|in:manager,employee';
            $rules['password'] = 'string|min:8';
        }
        $this->validate($request, $rules);
        if ($request->has('image_id')) {
            $user->image_id = $request->image_id;
        }
        if ($request->has('f_name')) {
            $user->f_name = $request->f_name;
        }
        if ($request->has('m_name')) {
            $user->m_name = $request->m_name;
        }
        if ($request->has('l_name')) {
            $user->l_name = $request->l_name;
        }
        if ($request->has('email')) {
            $user->email = $request->email;
        }
        if ($user->id == auth()->id()) {
            if ($request->has('new_password')) {
                $user->password = Hash::make($request->new_password);
            }
        } else {
            if ($request->has('role')) {
                $user->role = $request->role;
            }
            if ($request->has('password')) {
                $user->password = Hash::make($request->password);
            }
        }
        $user->save();
        return new UserResource($user);
    }
public function update ( Request $ request , $ id ) { if ( $ id === 'self' ) { $ user = auth ( ) -> user ( ) ; } else { $ user = User :: findOrFail ( $ id ) ; } $ this -> authorize ( 'update' , $ user ) ; $ rules = [ 'image_id' => 'nullable|integer|exists:images,id' , 'f_name' => 'string|max:191' , 'm_name' => 'nullable|string|max:191' , 'l_name' => 'string|max:191' , 'email' => [ 'string' , 'email' , 'max:191' , Rule :: unique ( 'users' ) -> ignore ( $ user ) , ] , ] ; if ( $ user -> id == auth ( ) -> id ( ) ) { $ rules [ 'cur_password' ] = 'required_with:new_password|password' ; $ rules [ 'new_password' ] = 'string|min:8|confirmed' ; } else { $ rules [ 'role' ] = 'string|in:manager,employee' ; $ rules [ 'password' ] = 'string|min:8' ; } $ this -> validate ( $ request , $ rules ) ; if ( $ request -> has ( 'image_id' ) ) { $ user -> image_id = $ request -> image_id ; } if ( $ request -> has ( 'f_name' ) ) { $ user -> f_name = $ request -> f_name ; } if ( $ request -> has ( 'm_name' ) ) { $ user -> m_name = $ request -> m_name ; } if ( $ request -> has ( 'l_name' ) ) { $ user -> l_name = $ request -> l_name ; } if ( $ request -> has ( 'email' ) ) { $ user -> email = $ request -> email ; } if ( $ user -> id == auth ( ) -> id ( ) ) { if ( $ request -> has ( 'new_password' ) ) { $ user -> password = Hash :: make ( $ request -> new_password ) ; } } else { if ( $ request -> has ( 'role' ) ) { $ user -> role = $ request -> role ; } if ( $ request -> has ( 'password' ) ) { $ user -> password = Hash :: make ( $ request -> password ) ; } } $ user -> save ( ) ; return new UserResource ( $ user ) ; }
Update the specified resource in storage.
Update the specified resource in storage .