JMeter Dude
JMeter Dude

Reputation: 329

How to get input values to save to variable in Laravel

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.

Upvotes: 1

Views: 3458

Answers (2)

hassan
hassan

Reputation: 8308

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']),
    ]);
}

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163948

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;

Upvotes: 1

Related Questions