28
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

tereško
  • 58,060
  • 25
  • 98
  • 150
myckhel
  • 800
  • 3
  • 15
  • 29

4 Answers4

55

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.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
4

DateTime is a PHP Object, so you can declare it using the slash before:

new \DateTime();

Or declaring it before you use and instantiating later:

use DateTime;

class Etc
{
    public function test()
    {
        $datetime = new DateTime();
    }
}
Rafael
  • 1,495
  • 1
  • 14
  • 25
4

Add a backslash \ (to define root namespace)

$dateTime = new \DateTime();

also you can use classes

use DateTime;
use DatePeriod;
use DateInterval;
Jordan D
  • 718
  • 7
  • 21
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
-1

By Using below Classes works for me.

use DateTime;
use DatePeriod;
use DateIntercal;
Chandan Sharma
  • 2,321
  • 22
  • 22