简体   繁体   English

Laravel controller 显示语法错误,意外变量“$user”,期待“function”或“const”

[英]Laravel controller shows syntax error, unexpected variable "$user", expecting "function" or "const"

I am creating a forgot password feature in my application and when creating my controller, I used this code:我正在我的应用程序中创建忘记密码功能,在创建 controller 时,我使用了以下代码:

$user = DB::table('users')->where('email', '=', $request->email)
->first();
//Check if the user exists
if (count($user) < 1) {
return redirect()->back()->withErrors(['email' => trans('User does not exist')]);
}

//Create Password Reset Token
DB::table('password_resets')->insert([
'email' => $request->email,
'token' => str_random(60),
'created_at' => Carbon::now()
]);
//Get the token just created above
$tokenData = DB::table('password_resets')
->where('email', $request->email)->first();

if ($this->sendResetEmail($request->email, $tokenData->token)) {
return redirect()->back()->with('status', trans('A reset link has been sent to your email address.'));
} else {
return redirect()->back()->withErrors(['error' => trans('A Network Error occurred. Please try again.')]);

But the first line gives an syntax error, unexpected variable "$user", expecting "function" or "const" error.但是第一行给出了一个syntax error, unexpected variable "$user", expecting "function" or "const"错误。 Does anyone know what I am doing wrong?有谁知道我做错了什么?

public function yourFunctionName(){
  $user = DB::table('users')->where('email', '=', $request->email)
                ->first();
    //Check if the user exists
    if ($user->count()< 1) {
       return redirect()->back()->withErrors(['email' => trans('User does not exist')]);
    }
    
    //Create Password Reset Token
    DB::table('password_resets')->insert([
      'email' => $request->email,
      'token' => str_random(60),
      'created_at' => Carbon::now()
    ]);
    enter code here
    //Get the token just created above
    $tokenData = DB::table('password_resets')
                 ->where('email', $request->email)->first();
    
    if ($this->sendResetEmail($request->email, $tokenData->token)) {
        return redirect()->back()
                        ->with('status', trans('A reset link has been sent to your email 
                         address.'));
    } else {
       return redirect()->back() 
                        ->withErrors(['error' => trans('A Network Error occurred. Please 
                      try again.')]);
    }

Make Sure Your { } are correct..and the code inside a function,check;确保您的 { } 是正确的..以及 function 中的代码,检查; are correct是正确的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析错误:语法错误、意外的“const”(T_CONST)、Laravel 项目中的预期变量(T_VARIABLE) - Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in Laravel project 语法错误,意外标记“&lt;&lt;”,需要“function”或“const” - syntax error, unexpected token "<<", expecting "function" or "const" Codeigniter:解析错误:Laravel 项目中的语法错误、意外的“const”(T_CONST)、期望变量(T_VARIABLE) - Codeigniter : Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in Laravel project 语法错误,意外的“请求”(T_STRING),期望 function (T_FUNCTION) 或 const (T_CONST) Laravel - syntax error, unexpected 'Request' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) Laravel FatalErrorException:语法错误,意外&#39;:&#39;,期待&#39;,&#39;或&#39;;&#39; 在Laravel - FatalErrorException : syntax error, unexpected ':', expecting ',' or ';' in Laravel ParseError:语法错误,意外的 'EntityManagerInterface' (T_STRING),期待 function (T_FUNCTION) 或 const (T_CONST) - ParseError: syntax error, unexpected 'EntityManagerInterface' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) 收到此错误“语法错误,意外的'回声'(T_ECHO),期待';' " 在 Laravel 中创建简单的控制器 - Getting this error "syntax error, unexpected 'echo' (T_ECHO), expecting ';' " in Laravel while creating simple controller Laravel 5:解析错误:语法错误,意外&#39;?&#39;,期待变量(T_VARIABLE) - Laravel 5 : Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) 语法错误,意外的T_FUNCTION,预期为&#39;)&#39; - syntax error, unexpected T_FUNCTION, expecting ')' 意外的“$twitter_handles”(T_VARIABLE),期望函数(T_FUNCTION)(在 Laravel 控制器中声明数组) - unexpected '$twitter_handles' (T_VARIABLE), expecting function (T_FUNCTION) (Declaring array in Laravel controller)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM