Laravel Error "Class 'App\Http\Controllers\DateTime' not found"

myckhel picture myckhel · Aug 29, 2018 · Viewed 27.9k times · Source
public function recover(Request $request){
    $email = $request->input('email');
    // Create tokens
    $selector = bin2hex(random_bytes(8));
    $token = random_bytes(32);

    $url = sprintf('%s',  route('recover.reset',['selector'=>$selector, 'validator'=>bin2hex($token)]));

    // Token expiration
    $expires = new DateTime('NOW');
    $expires->add(new DateInterval('PT01H')); // 1 hour

    // Delete any existing tokens for this user
    DB::delete('delete * from password_reset where email =?', $email);

    // Insert reset token into database
    $insert = $this->db->insert('password_reset', 
        array(
            'email'     =>  $email,
            'selector'  =>  $selector, 
            'token'     =>  hash('sha256', $token),
            'expires'   =>  $expires->format('U'),
        )
    );

Am trying to implement forgot password when the email form is submitted to forgotPasswordController it generates the below error

"Class 'App\Http\Controllers\DateTime' not found"

This is the picture of the controller the above code is not there is i cant modify it RecoverPasswordController Image

At the header i have tried using

use DateTime;

Or

use App\Http\Controllers\DateTime

But still generates same error please help fixing it. thanks in advance

Answer

delboy1978uk picture delboy1978uk · Aug 29, 2018

Above your class definition, import the class with a use statement.

use DateTime;

The alternative to that is to use the fully qualified namespace in your code. With PHP classes in the global namespace, all this means is a single backslash before the class name:

$expires = new \DateTime('NOW');

I prefer the first approach, as it allows you to see every class used in that file at a glance.