SQLSTATE [42S22]:找不到列:1054未知列-Laravel [英] SQLSTATE[42S22]: Column not found: 1054 Unknown column - Laravel

查看:127
本文介绍了SQLSTATE [42S22]:找不到列:1054未知列-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel框架.

I'm using the framework Laravel.

我有2个表(用户和成员).当我要登录时,收到错误消息:

I have 2 tables (Users and Members). When I want to login, I get the error message:

SQLSTATE [42S22]:找不到列:1054中的未知列'user_email' 'where子句'(SQL:从* members中选择*其中user_email =? 限制1)(绑定:数组(0 =>'test@hotmail.com',))

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_email' in 'where clause' (SQL: select * from members where user_email = ? limit 1) (Bindings: array ( 0 => 'test@hotmail.com', ))

表用户

CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
  `user_id` BIGINT NOT NULL AUTO_INCREMENT,
  `user_email` VARCHAR(45) NOT NULL,
  `user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_modified` TIMESTAMP NULL,
  `user_deleted` TIMESTAMP NULL,
  `user_lastlogin` TIMESTAMP NULL,
  `user_locked` TIMESTAMP NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
ENGINE = InnoDB;

表成员

CREATE TABLE IF NOT EXISTS `festival_aid`.`members` (
  `member_id` BIGINT NOT NULL AUTO_INCREMENT,
  `member_password` CHAR(32) NOT NULL,
  `member_salt` CHAR(22) NOT NULL,
  `member_token` VARCHAR(128) NULL,
  `member_confirmed` TIMESTAMP NULL,
  `user_id` BIGINT NOT NULL,
  PRIMARY KEY (`member_id`, `user_id`),
  INDEX `fk_members_users1_idx` (`user_id` ASC),
  CONSTRAINT `fk_members_users1`
    FOREIGN KEY (`user_id`)
    REFERENCES `festival_aid`.`users` (`user_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

迁移用户

public function up()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->increments('user_id');

            $table->string('user_email');
            $table->timestamp('user_created');
            $table->timestamp('user_modified');
            $table->timestamp('user_deleted');
            $table->timestamp('user_lastlogin');
            $table->timestamp('user_locked');
        });
    }

移民成员

public function up()
    {
        Schema::table('members', function(Blueprint $table)
        {
            $table->increments('member_id');

            $table->string('member_password');
            $table->string('member_salt');
            $table->string('member_token');

            $table->foreign('user_id')
                ->references('id')->on('users');
            //->onDelete('cascade');

            $table->timestamp('member_confirmed');
        });
    }

模型用户

class User extends Eloquent {

    protected $table = 'users';

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'user_id';

    public $timestamps = false;
}

模型成员

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class Member extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'members';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('member_password');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->member_password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'member_id';

    public $timestamps = false;

    public function users()
    {
        return $this->hasOne('User');
    }
}

成员模型使用: 使用Illuminate \ Auth \ UserInterface;

The Member model uses: use Illuminate\Auth\UserInterface;

<?php namespace Illuminate\Auth;

interface UserInterface {

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier();

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword();

}

控制器

public function store()
    {
        $input = Input::all();

        $rules = array('user_email' => 'required', 'member_password' => 'required');

        $v = Validator::make($input, $rules);

        if($v->passes())
        {
            $credentials = array('user_email' => $input['user_email'], 'member_password' => $input['member_password']);

            if(Auth::attempt($credentials))
            {
                return Redirect::to('/home');

            } else {

                return Redirect::to('login');
            }

        } else {

            return Redirect::to('login')->withErrors($v);
        }
    }

auth.php

return array(

    /*
    |--------------------------------------------------------------------------
    | Default Authentication Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the authentication driver that will be utilized.
    | This drivers manages the retrieval and authentication of the users
    | attempting to get access to protected areas of your application.
    |
    | Supported: "database", "eloquent"
    |
    */

    'driver' => 'eloquent',

    /*
    |--------------------------------------------------------------------------
    | Authentication Model
    |--------------------------------------------------------------------------
    |
    | When using the "Eloquent" authentication driver, we need to know which
    | Eloquent model should be used to retrieve your users. Of course, it
    | is often just the "User" model but you may use whatever you like.
    |
    */

    'model' => 'Member',

    /*
    |--------------------------------------------------------------------------
    | Authentication Table
    |--------------------------------------------------------------------------
    |
    | When using the "Database" authentication driver, we need to know which
    | table should be used to retrieve your users. We have chosen a basic
    | default value but you may easily change it to any table you like.
    |
    */

    'table' => 'members',

    /*
    |--------------------------------------------------------------------------
    | Password Reminder Settings
    |--------------------------------------------------------------------------
    |
    | Here you may set the settings for password reminders, including a view
    | that should be used as your password reminder e-mail. You will also
    | be able to set the name of the table that holds the reset tokens.
    |
    | The "expire" time is the number of minutes that the reminder should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'reminder' => array(

        'email' => 'emails.auth.reminder',

        'table' => 'password_reminders',

        'expire' => 60,

    ),

);

我在这里做错了什么?

What am I doing wrong here?

推荐答案

您已经配置了auth.php并使用members表进行身份验证,但是members表中没有user_email字段,因此Laravel说

You have configured the auth.php and used members table for authentication but there is no user_email field in the members table so, Laravel says

SQLSTATE [42S22]:找不到列:1054中的未知列'user_email' 'where子句'(SQL:从user_email =?limit的成员中选择* 1)(绑定:数组(0 =>'test@hotmail.com',))

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_email' in 'where clause' (SQL: select * from members where user_email = ? limit 1) (Bindings: array ( 0 => 'test@hotmail.com', ))

因为,它尝试匹配members表中的user_email,所以它不存在.根据您的auth配置,laravel使用的是members表而不是users表.

Because, it tries to match the user_email in the members table and it's not there. According to your auth configuration, laravel is using members table for authentication not users table.

这篇关于SQLSTATE [42S22]:找不到列:1054未知列-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆