PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] laravel variable not defined

 February 21, 2022     laravel, laravel-4, php     No comments   

Issue

This is weird. I am sure I am missing something simple. I have the following code:

$productToken = Input::get('key');
        $products = new Product;
        $userEmail = $products->activateProduct($productToken);

        $productDetailsArray = $products->getProductDetailsForUserEmail($productToken);

        $emailData = $productDetailsArray;
        Mail::send('emails.productReleased', $emailData, function($message){
            $message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');
        }); 

It is supposed to activate the product in the database and then send email to user. The email of the user is in the $userEmail and when I did var_dump, it shows. Somehow this line throws an error that $userEmail is undefined:

$message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');

This is the error I get:

Undefined variable: userEmail

I used Mail function before but instead of passing variable I passed the Input::get('email') because it was in a registration form. Right now, I don't have access to the Input but rather $userEmail. Please advise.


Solution

You are using a callback so function so at the time the function is called, the userEmail variable is certainly out of scope. You should send the userEmail variable to the function, maybe like this :

Mail::send('emails.productReleased', $emailData, function($message) use ($userEmail) {
    $message->to($userEmail)->subject('Notification - Your Product was Released to the Public!');
}); 

See https://www.php.net/manual/functions.anonymous.php#example-191 for information about lambda (anonymous) function and context.



Answered By - Holt
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing