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

时间:2018-05-23 17:06:05

标签: laravel laravel-5 laravel-4

我得到的错误是: (SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'users.post_id'(SQL:select * from users其中userspost_id = 1和{ {1}}。users不为空。)

我正在尝试查阅包含两个喜欢和不喜欢的帖子的帖子的页面:

---->表的迁移喜欢:

post_id

---->表用户的迁移是:

public function up()
{
    Schema::create('likes', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('post_id');
        $table->boolean('like');
        $table->timestamps();
    });
}

-------------------->我的用户模型是:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

    });
}

-------------->我的帖子模型是:

class User extends Authenticatable
{
use Notifiable;

public function likes() {

return $this->hasMany(User::class);
}
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
 }

----------->我喜欢的模特是:

 class Post extends Model
 {
public $table="posts";


 public function comments() {

return $this->hasMany(Comment::class)->orderBy('created_at');
}

 public function likes() {

return $this->hasMany(User::class);
 }
 }

-------------我的评论模型是:

 class Like extends Model
  {
 public function post() {

return $this->belongsTo(Post::class);
  }

 public function user() {

return $this->belongsTo(User::class);
 }

---->我在刀片Post.blade.php中调用变量的代码部分是:

 class Comment extends Model
 {

 protected $fillable = [
'username', 'body', 'post_id'
 ];
public function post() {

return $this->belongsTo(Post::Class);
 }

  }

我根据user.id和post.id手动将一些随机数插入到表格中,然后转为= 1

1 个答案:

答案 0 :(得分:0)

请更改帖子模型关系hasManyTo属于

返回$ this-> hasMany(User :: class);

用下面的代码替换,就可以了。

返回$ this-> belongsTo(User :: class);