31
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

5
  • 3
    Use new \DateTime() or Laravel's Carbon class Commented Aug 29, 2018 at 9:37
  • When you used use DateTime; in the header, the error was still Class 'App\Http\Controllers\DateTime' not found? Commented Aug 29, 2018 at 9:44
  • Yes it was still not found? Commented Aug 29, 2018 at 9:46
  • I cant modify the above code Commented Aug 29, 2018 at 9:47
  • 1
    I was able to fix it using namespace use DateTime; But I had 2 duplicate ForgotPasswordController in different directories so i had to locate the controller it was pointing to. Thank Y'all. Commented Aug 29, 2018 at 13:39

4 Answers 4

57

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.

Sign up to request clarification or add additional context in comments.

1 Comment

$currentTime = new \DateTime('now'); defining an like this directly working fine
6

Add a backslash \ (to define root namespace)

$dateTime = new \DateTime();

also you can use classes

use DateTime;
use DatePeriod;
use DateInterval;

Comments

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();
    }
}

Comments

1

By Using below Classes works for me.

use DateTime;
use DatePeriod;
use DateIntercal;

1 Comment

Works for laravel 11

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.