Home > Software engineering >  Laravel: ErrorException Undefined variable: ref
Laravel: ErrorException Undefined variable: ref

Time:05-29

please a new Laravel

I ran into the error: ErrorException Undefined variable: ref

I have gone through similar problems solved here but none of the solutions have worked for me.

The error is thrown from my RegisterController at line 52

Here is my controller code:

class RegisterController extends Controller

{ /** * Display register page. * * @return \Illuminate\Http\Response */ public function show() { return view('auth.register'); }

/**
 * Handle account registration request
 * 
 * @param RegisterRequest $request
 * 
 * @return \Illuminate\Http\Response
 */
public function register(RegisterRequest $request){
    $user = User::create($request->validated());
    auth()->login($user);
    
    $email = $user['email'];
    $fullname = $user['fullname'];

    Mail::send('email.welcome',
        ['password' => $request->input('password')],
        function($message) use ($email) {
            $message->to($email)
            ->subject("Welcome to Primval");
        }
    );


    if(!($user->referrer===null)){
        $referrer = $user->referrer;
        $ref=DB::table('users')->where('username', $referrer)->get();

        Mail::send('email.referral-notification', ['referrer'=>$ref], 
                function($message) use($request){
                    $message->to($ref->email);
                    $message->subject('Direct referral');
            });
    }




    return redirect('/dashboard')->with('success', "Account successfully registered.");
    }
}

CodePudding user response:

As stated by @tpojka:

Try with function($message) use ($request, $ref) {

In addition,

$ref=DB::table('users')->where('username', $referrer)->get();

The above line of code returns a collection.

You probably want a single row instead. I.e:

$ref= \App\Models\User::firstWhere('username', $referrer);

  • Related