How to use 2 updates function in Usercontroller in Laravel

Asked

Viewed 60 times

0

I’m trying to add an image of the user profile and also the btc wallet in an input. If I add the wallet I can’t add the profile image. I saw that the two use the update function and so I’m having conflict, if I remove the route of the wallet, the photo goes well, but I need the 2 ok.

My route:

Route::get('userp', 'UserController@index');
Route::post('userp', 'UserController@update');

My profile image controller:

public function update_avatar(Request $request){
        if ($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename ));

            $user = Auth::user();
            $user->avatar = $filename;
            $user->save();
        }

        return view('userp', array('userp' => Auth::user()) );

}

My wallet controller:

public function update(Request $request) {

        $data = \Input::all();

        $valida = [
            'passaport' => 'passaport',
            'nascimento' => 'date',
            'bitzpayer_email' => 'email'
        ];

        if (!empty($data['password'])) {
            $valida = array_merge($valida, ['password' => 'required|confirmed|min:6',]);
        }
        if (\Auth::user()->ativo == 0 && \Auth::user()->dataAtivacao == '0000-00-00') {
            $valida = array_merge($valida, ['pacote' => 'required|integer',]);
        }
        $validator = Validator::make($data, $valida);

        if ($validator->fails()) {

            return redirect('/userp')
                            ->withErrors($validator)
                            ->withInput();
        }
        if (!isset($data['saque'])) {

            if (!empty($data['bitzpayer_id'])) {
                if (!$this->checkAddress($data['bitzpayer_id'])) {
                    return redirect('/userp')->withErrors(['Invalid Wallet.']);
                }

            }

            $dados = [
                'bitzpayer_id' => @$data['bitzpayer_id'],
              ];
          } else {
            $dados = [
                'bitzpayer_id' => @$data['bitzpayer_id'],
             ];
        }

        if (\Auth::user()->ativo == 0 && \Auth::user()->dataAtivacao == '0000-00-00') {
            $dados['pacote'] = $data['pacote'];
        }
        if (!empty($data['password'])) {
            $senha = \DB::table('users')->where('id', \Auth::user()->id)->first()->password;
            if (\Hash::check($data['current_password'], $senha)) {
                $dados = array_merge($dados, ['password' => bcrypt($data['password']),]);
            } else {
                return redirect('userp')->withErrors(['Senha inválida, tente novamente.']);
            }
        }

        $user = \Auth::user()->update($dados);

        if ($user) { 

            return redirect()->back()->with('message', 'Your bitcoin wallet has been updated successfully!');
        } else {
            return redirect('/userp')->withErrors(['Não foi possivel atualizar suas informações, tente novamente.']);
        }
    }
  • 1

    You’re in trouble logic is not conflict.

  • seems to me something related to Submit, I’m not finding another way to send without using them

  • Atila can send all this in just one request ... !!! do not understand the problem is still too much code to debug!

  • @Atilaoliveira first of all I recommend that you read this https://answall.com/questions/247791/como-workcom-valida%C3%A7%C3%A3o-de-dados-no-Laravel to work in the best way possible the validations of your request and remove-the controller, after that it becomes easier to solve your problem with the update policy, because now realmetne is more complicated to know when the return will run simply by looking at the code

  • Cara has a lot of code so let’s organize this code does the following $request->validate([]) passes the fields that will validate and ends with this validation array and Validator, and exchanges this input::all() for $request->all();, as an update needs another parameter in the Function update that comes from the URL that is the user ID and you will use $user = User::find($id); to do the update. If you don’t understand from a touch I make the code as a response.

  • Po Thank you so much msm, but had already solved and do not know how to put this post as solved, but thank you so much for the help.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.