Search code examples
phpobjectparameter-passinglaravellaravel-3

Passing object to method laravel php bundles


Probably a noob question but here we go.

I'm using laravel and the messages bundle to send emails to verify users. But how do I get access to my $new_user object from within my send method?

    // Get inputdata and insert into table users
    $new_user = User::create(array(
        'username' => Input::get('username'), 
        'email' => Input::get('email'), 
        'password' => Hash::make(Input::get('password')),
        'hash' => hash('sha512', uniqid()) 
    ));

    Message::send(function($message)
    {
        $message->to('blablabla@hotmail.com');
        $message->from($new_user->email, $new_user->username);

        $message->subject('new account');
        $message->body('

            Bedankt voor het registreren!

            Uw account is aangemaakt, u kan inloggen met de volgende gegevens nadat u u account heeft geactiveerd met de url hier beneden.

            ------------------------
            Username: '.$new_user->username.'
            Password: '.$new_user->password.'
            ------------------------

            Klik op deze link om uw account te activeren.

            http://blablabla.dev/verify?email='.$new_user->email.'&hash='.$new_user->hash.' 

        ');
    });

Doing this obviously doesn't work since I don't have access to the object.

Hopefully I've phrased this right.


Solution

  • You can use the use keyword here

    $new_user = User::create(array(
        'username' => Input::get('username'), 
        'email' => Input::get('email'), 
        'password' => Hash::make(Input::get('password')),
        'hash' => hash('sha512', uniqid()) 
    ));
    
    Message::send(function($message) use ($new_user)
    {
        $message->to('blablabla@hotmail.com');
        $message->from($new_user->email, $new_user->username);
        // ...
    }