1

Kind of confused about how to capture values as an when they are saved to DB. This is laravel 5.4

In the app\Http\Controllers\Auth, I want to capture the input values that are being saved to the DB.

protected function create(array $data)
{

    Log::info('Adding the User ');  //  This is getting logged. 

    return User::create([
        'name' => $data['name'],
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

     $name = $data['name'];

     Log::info('Showing user profile for user: '.$name);  //  This is not getting logged. 
}

The reason why I want to capture is because I want to send a curl request with the captured input data to a third party application as soon as a user is created.

Thank you.

1
  • 1
    you can not do this after your return statement Commented Mar 30, 2017 at 18:33

2 Answers 2

1

you can not executes any thing after your returning statement,

If called from within a function, the return statement immediately ends execution of the current function,

rather, you will need to store it in a variable then return it after doing your logging, something like follows :

protected function create(array $data)
{

    Log::info('Adding the User ');  //  This is getting logged. 

    $createdUser = User::create([
        'name' => $data['name'],
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

     $name = $createdUser->name;

     Log::info('Showing user profile for user: '.$name);  //  This is not getting logged.

     return $createdUser;
}

update

as long as you don't need a returned data from your DB, you may simple move this two lines :

$name = $data['name'];

Log::info('Showing user profile for user: '.$name);

before returning the creation object :

protected function create(array $data)
{

    Log::info('Adding the User ');  //  This is getting logged. 

    $name = $data['name'];

     Log::info('Showing user profile for user: '.$name);  //  This is not getting logged. 

    return User::create([
        'name' => $data['name'],
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of returning created User instance do this:

$user = User::create([
    'name' => $data['name'],
    'username' => $data['username'],
    'email' => $data['email'],
    'password' => bcrypt($data['password']),
]);

// Do stuff here
$name = $user->name;

return $user;

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.