如何获取SQLSTATE[42S22]:未找到列:1054未知列'$电子邮件';在';其中条款'


how to get SQLSTATE[42S22]: Column not found: 1054 Unknown column '$email' in 'where clause' solved in my case

注意:-我是拉拉威尔世界的新手。

我有一个作为的迁移

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table)
        {
            $table-> increments('id');
            $table-> string('email');            
            $table-> string('username');            
            $table-> string('password');
            $table-> string('first_name')-> nullable();
            $table-> string('last_name')-> nullable();
            $table-> string('location')-> nullable();
            $table-> string('remember_token')-> nullable();            
            $table-> timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

现在我想使用表单实现登录功能

<form method="post" action="{{route('auth.signin')}}">                      
    <input type="text" name="login_email" class="header-login-input z-depth-2" placeholder="email"/>
    <input type="password" name="login_password" class="header-login-input z-depth-2" placeholder="password"/>
    <input type="hidden" name="_token" value="{{ Session::token()}}">
    <button name="action">Login</button>
</form>

此时,我的登录控制器是

class LoginAuthController extends Controller
{
    public function postSignIn(Request $request)
    {
        $this -> validate($request, [           
            'login_email' => 'required',
            'login_password' => 'required',
            ]);
        if (!Auth::attempt($request->only(['login_email',
            'login_password']))) {
            return  redirect()-> back()-> with('info', "Could not sign you with those details!!!");
        }       
        return  redirect()-> route('home') -> with('info', "Welcome to home, You are signed in");
    }}

当我运行应用程序时,我得到错误

QueryException in Connection.php line 673:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'login_email' in 'where clause' (SQL: select * from `users` where `login_email` = some@wi.con limit 1)

此时,我在postSignIn()中做了一个更改,我写了

$email = $request->input('login_email');
        $password = $request->input('login_password');
        if (!Auth::attempt($request->only(['$email',
            '$password']))) {
            return  redirect()-> back()-> with('info', "Could not sign you with those details!!!");
        }

但仍获得

QueryException in Connection.php line 673:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '$email' in 'where clause' (SQL: select * from `users` where `$email` is null limit 1)

问题

我的问题是如何将值传递给

Auth::attempt($request->only(['$email', '$password']))

这样我的问题就可以解决了。

更新

如果我将登录表单更改为

 <form method="post" action="{{route('auth.signin')}}">                      
        <input type="text" name="email" class="header-login-input z-depth-2" placeholder="email"/>
        <input type="password" name="password" class="header-login-input z-depth-2" placeholder="password"/>
        <input type="hidden" name="_token" value="{{ Session::token()}}">
        <button name="action">Login</button>
    </form>

然后这就起作用了,还有一个问题是,同一页上有注册和登录表格,在验证时,这两个表格之间存在冲突,请提供任何帮助。

让我们阅读错误消息

Connection.php中的QueryException第673行:SQLSTATE[42S22]:列不是找到:1054"where子句"中的未知列"login_email"(SQL:从users中选择*,其中login_email=some@wi.con限制1)

它表示users表中没有login_email列。这是非常真实的。在迁移中,您创建了一个名为email的不同列。

检查Laravel手动

public function authenticate()
{
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}

之后的解释

因此,在上面的示例中,用户将通过电子邮件列。如果找到用户,则散列密码存储在数据库将与传递给的哈希密码值进行比较该方法通过数组。如果两个散列密码与将为用户启动经过身份验证的会话。

代码应该是这样的(我不容易检查,这是我的猜测)

if (Auth::attempt([
    'email' => $request->input('login_email'),
    'password' => $request->input('login_password')
])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }

请注意,正如文档中所解释的那样,您提供了一个具有关键字emailpassword的数组,它们与您的DB结构和表单中的正确值相匹配。

$email和$password设置为字符串,带有'而不是带有".

有2个选项

$request->only(["$email","$password"])
// or
$request->only([$email,$password]) // this is the best option

示例:

$test = 54;
echo '$test'; // return $test
echo "$test"; // return 54
echo $test; // return 54

您应该使用"当您有带变量的文本时,如:

echo "Number = $test";