未定义的变量:用户
Undefined Variable : user
我正在尝试发送带有激活码的消息。我有一张注册表。表单将数据发送到控制器,控制器将数据保存到数据库中。但是在将数据保存到配置文件 table 后,它以某种方式无法获取 $user 变量。
这是我的控制器:
DB::transaction(function() use($first_name,$last_name,$email,
$password,$address,$phone,$country_id,$state,$city,
$zip_code,$skype,$birth_date,$code){
//add info to user table
$user = new User;
$user->username = $email;
$user->password = $password;
$user->email = $email;
$user->first_name = $first_name;
$user->last_name = $last_name;
$user->active = 0;
$user->code = $code;
$user->save();
//Get the user ID ceated just now
$new_users = $new_user->id;
// add information to Profile table
$profile = new Profile;
$profile->user_id = $new_users;
$profile->phone = $phone;
$profile->address = $address;
$profile->country_id = 1;
$profile->state = $state;
$profile->skype = $skype;
$profile->city = $city;
$profile->zip_code = $zip_code;
$profile->birth_date = $birth_date;
$profile->timezone_id = 1;
$profile->save();
});//inside a transaction
if($user){
Mail::send('emails.welcome',
array('link'=> URL::route('account-activate', $code),'user'=>$user->first_name),
function($message) use ($user) {
$message->to($user->email , $user->user)->from('admin@spandango.net')->subject('Active your account !');
}
);
return Redirect::back()
->with('message' , 'Your account is created ! Please check you email to activate your account !'); }
return Redirect::to('/message');
我认为我的代码是绝对正确的。你能解释一下为什么它没有定义 $user 变量吗?
那是因为 $user 在那个数据库事务块中;它超出了右括号的范围。定义在交易之上
这是因为 $user 变量的范围。在 DB::transaction(function() {});
上面定义 $user
解决方案
复制并替换上面的内容。
$user = false;
DB::transaction(function() use($first_name,$last_name,$email,
$password,$address,$phone,$country_id,$state,$city,
$zip_code,$skype,$birth_date,$code,$user){
//add info to user table
$user = new User;
$user->username = $email;
$user->password = $password;
$user->email = $email;
$user->first_name = $first_name;
$user->last_name = $last_name;
$user->active = 0;
$user->code = $code;
$user->save();
//Get the user ID ceated just now
$new_users = $new_user->id;
// add information to Profile table
$profile = new Profile;
$profile->user_id = $new_users;
$profile->phone = $phone;
$profile->address = $address;
$profile->country_id = 1;
$profile->state = $state;
$profile->skype = $skype;
$profile->city = $city;
$profile->zip_code = $zip_code;
$profile->birth_date = $birth_date;
$profile->timezone_id = 1;
$profile->save();
});//inside a transaction
if($user){
Mail::send('emails.welcome',
array('link'=> URL::route('account-activate', $code),'user'=>$user->first_name),
function($message) use ($user) {
$message->to($user->email , $user->user)->from('admin@spandango.net')->subject('Active your account !');
}
);
return Redirect::back()
->with('message' , 'Your account is created ! Please check you email to activate your account !'); }
return Redirect::to('/message');
尽管定义
$user = false;
以上
DB::transaction
暂时可以解决这个问题,但我不会推荐它作为一种好的做法,最好的解决方案是使用
Mail::send()
在数据库事务块中,您也可以改进代码,上面的代码应该是
DB::transaction(function() use($first_name,$last_name,$email,
$password,$address,$phone,$country_id,$state,$city,
$zip_code,$skype,$birth_date,$code,$user){
//add info to user table
$user = new User;
$user->username = $email;
$user->password = $password;
$user->email = $email;
$user->first_name = $first_name;
$user->last_name = $last_name;
$user->active = 0;
$user->code = $code;
$user->save();
//Get the user ID ceated just now
$new_users = $new_user->id;
// add information to Profile table
$profile = new Profile;
$profile->user_id = $new_users;
$profile->phone = $phone;
$profile->address = $address;
$profile->country_id = 1;
$profile->state = $state;
$profile->skype = $skype;
$profile->city = $city;
$profile->zip_code = $zip_code;
$profile->birth_date = $birth_date;
$profile->timezone_id = 1;
$profile->save();
if( !$profile || !$user )
{
return Redirect::back()
->with('message' , 'Your account is created ! Please check you email to activate your account !');
} else {
// Else commit the queries
Mail::send('emails.welcome',
array('link'=> URL::route('account-activate', $code),'user'=>$user->first_name),
function($message) use ($user) {
$message->to($user->email , $user->user)->from('admin@spandango.net')->subject('Active your account !');
}
);
}
});//inside a transaction
我正在尝试发送带有激活码的消息。我有一张注册表。表单将数据发送到控制器,控制器将数据保存到数据库中。但是在将数据保存到配置文件 table 后,它以某种方式无法获取 $user 变量。
这是我的控制器:
DB::transaction(function() use($first_name,$last_name,$email,
$password,$address,$phone,$country_id,$state,$city,
$zip_code,$skype,$birth_date,$code){
//add info to user table
$user = new User;
$user->username = $email;
$user->password = $password;
$user->email = $email;
$user->first_name = $first_name;
$user->last_name = $last_name;
$user->active = 0;
$user->code = $code;
$user->save();
//Get the user ID ceated just now
$new_users = $new_user->id;
// add information to Profile table
$profile = new Profile;
$profile->user_id = $new_users;
$profile->phone = $phone;
$profile->address = $address;
$profile->country_id = 1;
$profile->state = $state;
$profile->skype = $skype;
$profile->city = $city;
$profile->zip_code = $zip_code;
$profile->birth_date = $birth_date;
$profile->timezone_id = 1;
$profile->save();
});//inside a transaction
if($user){
Mail::send('emails.welcome',
array('link'=> URL::route('account-activate', $code),'user'=>$user->first_name),
function($message) use ($user) {
$message->to($user->email , $user->user)->from('admin@spandango.net')->subject('Active your account !');
}
);
return Redirect::back()
->with('message' , 'Your account is created ! Please check you email to activate your account !'); }
return Redirect::to('/message');
我认为我的代码是绝对正确的。你能解释一下为什么它没有定义 $user 变量吗?
那是因为 $user 在那个数据库事务块中;它超出了右括号的范围。定义在交易之上
这是因为 $user 变量的范围。在 DB::transaction(function() {});
上面定义 $user解决方案
复制并替换上面的内容。
$user = false;
DB::transaction(function() use($first_name,$last_name,$email,
$password,$address,$phone,$country_id,$state,$city,
$zip_code,$skype,$birth_date,$code,$user){
//add info to user table
$user = new User;
$user->username = $email;
$user->password = $password;
$user->email = $email;
$user->first_name = $first_name;
$user->last_name = $last_name;
$user->active = 0;
$user->code = $code;
$user->save();
//Get the user ID ceated just now
$new_users = $new_user->id;
// add information to Profile table
$profile = new Profile;
$profile->user_id = $new_users;
$profile->phone = $phone;
$profile->address = $address;
$profile->country_id = 1;
$profile->state = $state;
$profile->skype = $skype;
$profile->city = $city;
$profile->zip_code = $zip_code;
$profile->birth_date = $birth_date;
$profile->timezone_id = 1;
$profile->save();
});//inside a transaction
if($user){
Mail::send('emails.welcome',
array('link'=> URL::route('account-activate', $code),'user'=>$user->first_name),
function($message) use ($user) {
$message->to($user->email , $user->user)->from('admin@spandango.net')->subject('Active your account !');
}
);
return Redirect::back()
->with('message' , 'Your account is created ! Please check you email to activate your account !'); }
return Redirect::to('/message');
尽管定义
$user = false;
以上
DB::transaction
暂时可以解决这个问题,但我不会推荐它作为一种好的做法,最好的解决方案是使用
Mail::send()
在数据库事务块中,您也可以改进代码,上面的代码应该是
DB::transaction(function() use($first_name,$last_name,$email,
$password,$address,$phone,$country_id,$state,$city,
$zip_code,$skype,$birth_date,$code,$user){
//add info to user table
$user = new User;
$user->username = $email;
$user->password = $password;
$user->email = $email;
$user->first_name = $first_name;
$user->last_name = $last_name;
$user->active = 0;
$user->code = $code;
$user->save();
//Get the user ID ceated just now
$new_users = $new_user->id;
// add information to Profile table
$profile = new Profile;
$profile->user_id = $new_users;
$profile->phone = $phone;
$profile->address = $address;
$profile->country_id = 1;
$profile->state = $state;
$profile->skype = $skype;
$profile->city = $city;
$profile->zip_code = $zip_code;
$profile->birth_date = $birth_date;
$profile->timezone_id = 1;
$profile->save();
if( !$profile || !$user )
{
return Redirect::back()
->with('message' , 'Your account is created ! Please check you email to activate your account !');
} else {
// Else commit the queries
Mail::send('emails.welcome',
array('link'=> URL::route('account-activate', $code),'user'=>$user->first_name),
function($message) use ($user) {
$message->to($user->email , $user->user)->from('admin@spandango.net')->subject('Active your account !');
}
);
}
});//inside a transaction