Laravel SQLSTATE [42S22]:找不到列:1054未知列

时间:2019-05-15 10:30:07

标签: mysql database laravel

我正在尝试与讲师表和用户表建立关系。这是create_lecturers_table的代码

public function up()
    {
        Schema::create('lecturers', function (Blueprint $table) {
            $table->bigIncrements('id');


            $table->integer('class03');
            $table->integer('class03_stream');
            $table->date('class03_from');
            $table->date('class03_to');

            $table->string('remarks')->nullable();

            $table->integer('created_user_id')->unsigned()->nullable();
            $table->integer('updated_user_id')->unsigned()->nullable();
            $table->foreign('created_user_id')->references('id')->on('users');
            $table->foreign('updated_user_id')->references('id')->on('users');
            $table->timestamps();
        });
    }

这是讲师模型

class Lecturer extends Model
{
    protected $table = 'lecturers';

    protected $fillable = [

        'class03', 'class03_stream' ,'class03_from','class03_to','remarks','created_user_id','updated_user_id',

    ];

    public function user(){
      return $this->hasMany('PMS\User');
    }
}

这是控制器的讲师索引功能

public function index(Lecturer $model)
    {
        return view('lecturers.index',['lecturers' => $model->paginate(15)]);
    }

这是create_users_table

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('user');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('usertype');
            $table->boolean('activestatus')->default(false);
            $table->rememberToken();
            $table->timestamps();

        });
    }

这是用户模型


class User extends Authenticatable
{
    use Notifiable;



    public function lecturer(){
        return $this->belongsTo('PMS\Lecturer');
    }


    protected $fillable = [
        'name', 'email', 'password','usertype',
    ];

我想做的就是查看将通过System创建讲师的用户名。这样,我就可以在view.blade.php中回显用户名,如下所示:

<td>{{ $lecturer->user->name }}

当我进入视图时,会产生此错误。

ErrorException (E_ERROR)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.lecturer_id' in 'where clause' (SQL: select * from `users` where `users`.`lecturer_id` = 1 and `users`.`lecturer_id` is not null) (View: E:\BIT FINAL YEAR PROJECTS\20190419-using-template\resources\views\lecturers\index.blade.php)

有人可以告诉我怎么了。 谢谢

1 个答案:

答案 0 :(得分:0)

我认为您的口才不好,处事方式错误。尝试将它们更改如下:

讲师

public function user(){
    return $this->belongsTo('PMS\User', 'created_user_id');
}

用户

public function lecturers(){
    return $this->hasMany('PMS\Lecturer', 'created_user_id');
}