Laravel错误“找不到类'App \ Http \ Controllers \ DateTime'”

myc*_*hel 5 php post laravel laravel-5.6

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'),
        )
    );
Run Code Online (Sandbox Code Playgroud)

我试图在将电子邮件表单提交给forgotPasswordController时实施忘记密码,它会产生以下错误

“找不到类'App \ Http \ Controllers \ DateTime'”

这是控制器的图片,上面的代码不存在,我无法对其进行修改 。RecoverPasswordController Image

在标题我尝试使用

use DateTime;
Run Code Online (Sandbox Code Playgroud)

要么

use App\Http\Controllers\DateTime
Run Code Online (Sandbox Code Playgroud)

但是仍然会产生相同的错误,请帮助修复它。提前致谢

del*_*8uk 9

在类定义上方,使用use语句导入类。

use DateTime;
Run Code Online (Sandbox Code Playgroud)

替代方法是在代码中使用完全限定的名称空间。对于全局名称空间中的PHP类,所有这些都意味着在类名之前加一个反斜杠:

$expires = new \DateTime('NOW');
Run Code Online (Sandbox Code Playgroud)

我更喜欢第一种方法,因为它使您可以一目了然地看到该文件中使用的每个类。