-1

I have a Laravel 8 project. There is a feature to approve incoming creator references. However, when I press the create button when we want to approve the incoming applications, 500 | It gives a SERVER ERROR error.

When I look from Sentry, it shows the error as follows:

ErrorException creators_store

Undefined variable $user

enter image description here

CreatorController::store

    public function store(Request $request){
        $request->validate([
            'name' => 'required',
            'email' => 'required|email'
        ]);

        $subscriber = User::where('email','=',$request->email)->get()->first();

        $form_id = $request->form_id;

        if($subscriber!=null){
            // Already registered user
            $subscriber->update([
               'role_id' => 3,
                'name' => $request->name,
            ]);

            $subscriber = User::findorfail($subscriber->id);

            $subscriber->profile_token = env('FRONTEND_PATH').'creator-profile-form?token='.base64_encode($subscriber->email.'/'.$subscriber->id);
            Mail::send(['html'=>'email.creators.register'], ['creator' => $subscriber],  function($message) use($subscriber){
                $message->to($subscriber->email, $subscriber->name)->subject
                ('Welcome to the Creator Community');
                $message->from('creatorcommunity@omnicourse.io','Omnicourse Creator Community');
            });

        }else{
            // New Creator
            $pass = random_int(100000, 999999);

            $user = User::create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($pass),
                'role_id' => 3,
            ]);

        }


        if($form_id!=null){
            //Formdan geldi
            $form = CreatorApplicationForm::findorfail($form_id);
            $form->update([
               'creator_id' => $user->id
            ]);
        }

        return redirect('/admin-panel/creators');
    }

It was giving a similar error before, but it was because that mailgun service was not paid for. We paid for the service, but this time it started to give an error like this, but we used the same codes before and it did not give an error. I don't understand why it gives an error even though we haven't made any code changes.

Anyone know the reason or solution for this?

iguner
  • 51
  • 8
  • 1
    You are only creating `$user` in that first else branch - if it does not go into that, then there simply _is_ no `$user` in the following code. – CBroe Dec 14 '22 at 11:33
  • @CBroe How do you think I should update it? – iguner Dec 14 '22 at 11:36
  • How should I know? I don't know what you actually need to achieve there. Maybe you need to find an existing user elsewhere, maybe you need to _not_ update the form if it went into the first if branch ... impossible for us to tell at this point. – CBroe Dec 14 '22 at 11:39
  • Maybe it simple needs to be the `$subscriber` form the if block then in that case. – CBroe Dec 14 '22 at 11:40

1 Answers1

0

The issue here is that you are creating a user in the else statement, so if it never reaches the else statement (because it is true) you will never have $user.

You can either rewrite your controller somehow or do something like

 if(!isset($user)){
   $user = [some universal hardcode];
 }
devsead
  • 312
  • 7