what's the code your calling that's giving you that error?
'SQLSTATE[42S22]: Column not found: 1054 Unknown column
Hey what's up, I've been getting the following error when trying to attach or retrieve "courses" from "users".
I have a manyToMany relationship and I have a course_id pivot table.
The error I'm getting is:
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'courses.user_id' in 'where clause' (SQL: select * from `courses` where `courses`.`user_id` = 1 and `courses`.`user_id` is not null)'
I had an "enrollments" table that was acting as a pivot table but I decided to delete it so I deleted the migration and made "course_user" migration. I've tried various things like forcing the "course_user" on the model relationship, I've migrated:refresh several times. I also deleted the enrollments migration on the database.
My User, Course and migration files are as follow:
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('email')->unique();
$table->string('password', 60);
$table->boolean('is_premium')->default(0);
$table->boolean('is_confirmed')->default(0);
$table->timestamp('activated_at');
$table->string('confirmation_code', 80)->nullable();
$table->tinyInteger('questions_left')->default(4);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
public function up()
{
Schema::create('courses', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->string('description');
$table->tinyInteger('lessons_count');
$table->string('running_time');
$table->integer('price');
$table->boolean('is_free')->default(0);
$table->boolean('is_available')->default(0);
$table->boolean('is_complete')->default(0);
$table->tinyInteger('completeness');
$table->string('features');
$table->string('course_url');
$table->string('course_slug');
$table->string('registration_url');
$table->string('cover_image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('courses');
}
}
public function up()
{
Schema::create('course_user', function(Blueprint $table)
{
$table->increments('id');
$table->integer('course_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('course_id')->references('id')->on('courses');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('course_user');
}
}
The Models:
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['username', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
/* Relaciones de Eloquent*/
public function profiles()
{
return $this->hasOne('Profile');
}
public function roles()
{
return $this->belongsToMany('Role');
}
public function courses()
{
return $this->hasMany('Course');
}
/* Funciones propias */
public function hasRole($name)
{
foreach($this->roles as $role)
{
if($role->name == $name) return true;
}
return false;
}
}
class Course extends Model {
protected $table = 'courses';
protected $fillable = [];
/* Relaciones de Eloquent */
public function users()
{
return $this->belongsToMany('User');
}
public function lessons()
{
return $this->hasMany('Lesson');
}
}
I can't seem to find the mistake. I've done composer dump-autoload a couple of times. I've tried changing the relations between the Models (between belongsToMany and hasMany.
Sorry for the long post but I've spent almost 3 hours trying to solve this and this is the last resource.
Please or to participate in this conversation.