Laravel 5 SQLSTATE[42S22]:未找到列:“字段列表”中的 1054 列“user_id”未知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28597762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list'
提问by Eduardo Guimar?es
I'm having problems when I try to create a new project logged as a user. My old table 'projects' had a column called user_id, and I decided to delete it, because laravel 5 do it automatically. So, I refresh the migrations with the correct projects table (without the 'user_id' column), but when I try to create the new project, I got this error:
当我尝试创建以用户身份登录的新项目时遇到问题。我的旧表“projects”有一个名为 user_id 的列,我决定删除它,因为 laravel 5 会自动删除它。因此,我使用正确的项目表(没有“user_id”列)刷新迁移,但是当我尝试创建新项目时,出现此错误:
QueryException in Connection.php line 614:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list' (SQL: insert into `projects` (`name`, `slug`, `user_id`, `updated_at`, `created_at`) values (Project 1, project-1, 1, 2015-02-18 23:36:28, 2015-02-18 23:36:28))
Its really odd, cause I didn't find the 'user_id' in any files that 'projects' is related. I checked the workbench and the table is ok. Here's the function that I call to store de project:
这真的很奇怪,因为我没有在任何与“项目”相关的文件中找到“user_id”。我检查了工作台,桌子没问题。这是我调用来存储项目的函数:
public function store(Request $request)
{
Auth::user()->projects()->create($request->all());
return Redirect::route('projects.index')->with('message', 'Project created!');
}
It seems that the laravel stored my old 'projects' table and did not refresh when I did it. I already updated the composer an dump-autoload, but the error persists. Here's the part of table creation on the migration file:
似乎 laravel 存储了我旧的“项目”表,并且在我这样做时没有刷新。我已经更新了 Composer 转储自动加载,但错误仍然存在。这是迁移文件上的表创建部分:
Schema::create('projects', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
There's no 'user_id' column. Here's my Project model:
没有“user_id”列。这是我的项目模型:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model {
protected $table = 'projects';
protected $guarded = [];
public function tasks()
{
return $this->hasMany('App\Task');
}
public function users()
{
return $this->belongsTo('App\User');
}
}
The Task model is the same, except in the relationship parts. And the User model I'm using the default one, only adding the relationships.
除了关系部分之外,任务模型是相同的。而我使用的是默认的 User 模型,只添加了关系。
I appreciate any help related.
我感谢任何相关的帮助。
采纳答案by Marcin Nabia?ek
The problem here is that you shouldhave user_idcolumn for your projects table. Laravel won't alter your DB schema automatically
这里的问题是你的项目表应该有user_id列。Laravel 不会自动更改您的数据库架构
When you run:
当你运行时:
Auth::user()->projects()->create($request->all());
Laravel tries to do something like that:
Laravel 尝试做这样的事情:
Project::create(array_merge($request->all(), 'user_id' => Auth::user()->id));
So Laravel will try to assign the newly created project to the user you gave him (in this case Auth::user()). So you in this case you need to have user_idcolumn because you even defined relationship:
所以 Laravel 会尝试将新创建的项目分配给你给他的用户(在这种情况下Auth::user())。所以在这种情况下你需要有user_id列,因为你甚至定义了关系:
public function users()
{
return $this->belongsTo('App\User');
}
so the default column to bind project to user will be user_idand it's needed in your schema to work.
因此将项目绑定到用户的默认列将是,user_id并且您的架构需要它才能工作。

