Laravel 5 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list'

13,191

The problem here is that you should have user_id column for your projects table. Laravel won't alter your DB schema automatically

When you run:

Auth::user()->projects()->create($request->all());

Laravel tries to do something like that:

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_id column because you even defined relationship:

public function users()
{
    return $this->belongsTo('App\User');
}

so the default column to bind project to user will be user_id and it's needed in your schema to work.

Share:
13,191
Eduardo Guimarães
Author by

Eduardo Guimarães

Updated on June 04, 2022

Comments

  • Eduardo Guimarães
    Eduardo Guimarães almost 2 years

    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:

    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:

        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:

        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:

    <?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.

    I appreciate any help related.