user12897186
user12897186

Reputation:

Laravel 'Undefined variable:' in User Controller

I want to sent thanks mail when users regist our service. But, This code is error Undefined variable: fhbvuileb in $message->to($fhbvuileb). Help ME!!!

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;

class RegisterController extends Controller
{

  /** ~~~ **/

  protected function create(array $data)
  {

    $fhbvuileb = $data['email'];
    Mail::send('emails.user_register', ["message" => "Hello!"], function($message) {
      $message->to($fhbvuileb)
              ->subject("Thank you!!!");
    });
    return User::create([
      'name' => $data['name'],
      'email' => $data['email'],
      'password' => Hash::make($data['password']),
    ]);

  }
}

Upvotes: 0

Views: 474

Answers (2)

Md Abdul Awal
Md Abdul Awal

Reputation: 522

You forgot to use language construct use() to pass any argument from the parent scope

   Try this:
    Mail::send('emails.user_register', ["message" => "Hello!"], function($message) use($fhbvuileb) {
              $message->to($fhbvuileb)
                      ->subject("Thank you!!!");
            });

Upvotes: 0

Rehan
Rehan

Reputation: 4013

You need to use the variable inside the Mail callback function. What you have written is called Closure, and hence the variable is not available inside the closure function scope.

There is a keyword in PHP called use which makes this $fhbvuileb inside the function.

$fhbvuileb = $data['email'];
    Mail::send('emails.user_register', ["message" => "Hello!"], function($message) use($fhbvuileb) {
      $message->to($fhbvuileb)
              ->subject("Thank you!!!");
    });

Upvotes: 2

Related Questions