1

I am trying to send message with activation code. I have an registration form. The forms sends data to the controller and the controller saves the data into database . But somehow it fails to grab the $user variable after saving data into profiles table.

Here is my controller:

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('[email protected]')->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');

I think my code is absolutely right. Can you please explain why it fails to define the $user variable ?

4 Answers 4

2

This is because of the scope of the $user variable. Define $user above DB::transaction(function() {});

Sign up to request clarification or add additional context in comments.

Comments

1

Though defining

$user = false;

above

DB::transaction

might do the trick for now but I won't recommend it as a good practice the best solution would be to use the

Mail::send()

inside the Database transaction block and you might can improve your code as well and the above code should be

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('[email protected]')->subject('Active your account !');
        }
    );
}

});//inside a transaction

Comments

1

That is because $user is inside that DB transaction block; it goes out of scope with the closing brace. Define it above the transaction

1 Comment

Hey man , If I define that above the DB transaction block it'll never grab the variable. Did you check it ?
1

Solution

Copy & Replace above.

$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('[email protected]')->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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.