【问题标题】:How to connect 'users' table with 'post' table in laravel?如何在 laravel 中将“用户”表与“帖子”表连接起来?
【发布时间】:2017-05-15 16:46:25
【问题描述】:

这是我的结构,我想在laravel中连接这两个表,怎么做?

发布表:

public function up()
{
    Schema::create('post', function (Blueprint $table) {
        $table->increments('post_id');
        $table->string('post');
        $table->integer('time');
        $table->string('host');
        $table->integer('vote_up');
        $table->integer('vote_down');
        $table->foreign('id_fk')->references('id')->on('users');
        $table->timestamps();
    });
}

用户表:

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

【问题讨论】:

  • “连接这两个表”是什么意思?您只想在模型之间创建关系吗?

标签: database laravel database-connection laravel-facade


【解决方案1】:

我认为您只是在迁移中粘贴,但您确实需要在您的 posts之前创建您的 users 表。我会改变

$table->foreign('id_fk')->references('id')->on('users');

$table->foreign('user_id')->references('id')->on('users');

因为 Laravel 可以推断外键:

Eloquent 通过检查名称来确定默认的外键名称 关系方法的名称,并在方法名称后面加上 _id。 但是,如果 Post 模型上的外键不是 user_id,你 可以将自定义键名作为第二个参数传递给 belongsTo 方法

然后,您在模型中需要的只是以下内容:

class Post extends Model
{
    /**
     * Get the user that owns the post.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
        // if you want to keep your current structure:
        // return $this->belongsTo('App\User', 'id_fk', 'id);
    }
}

class User extends Model
{
    /**
     * Get the post for a user.
     */
    public function posts()
    {
        return $this->hasMany('App\Post');
        // if you want to keep your current structure:
        // return $this->belongsTo('App\Post', 'id_fk');
    }
}

你可以阅读更多关于建立关系here

【讨论】:

  • 非常感谢,但我删除了我的数据库并使用其他名称重新创建它,现在它显示“[Illuminate\Database\QueryException] SQLSTATE[HY000] [1049] Unknown database 'chatty' (SQL : select * from information_schema.tables where table_schema = chatty and table_name = migrations) [PDOException] SQLSTATE[HY000] [1049] Unknown database 'chatty' " (chatty 是我以前的数据库)
  • 这可能最好作为一个单独的问题,但您是否更改了您的config/database.php
  • 不,那是我的错误,对不起,我现在发现了错误,但回到我之前的问题,我像你评论的那样尝试过,但它不起作用
  • 类 Post 扩展迁移{ 使用 Illuminate\Database\Eloquent\Model; public function up(){ Schema::create('post', function (Blueprint $table) { $table->increments('post_id');$table->string('post');$table->integer( 'time');$table->string('host');$table->integer('vote_up');$table->integer('vote_down');$table->foreign('user_id')->引用('id')->on('users');$table->timestamps();});} public function down(){ Schema::dropIfExists('post');} public function user(){ return $this->belongsTo('App\User');}}
  • 请用代码更新您的问题,这样阅读非常困难。另外请添加您遇到的错误或问题。
【解决方案2】:

在您的帖子表中,您应该有:

$table->integer('user_id')->unsigned();

在您的用户模型上:

public function posts(){
return $this->hasMany(Post::class);
}

在您的 Post 模型上:

public function user(){
return $this->belongsTo(User::class);
}

【讨论】:

  • 或者你可以保留你的表结构并在关系上定义外键:public function posts(){ return $this->hasMany(Post::class, 'id_fk'); }public function user(){ return $this->belongsTo(User::class, 'id_fk'); }
  • 非常感谢,但我删除了我的数据库并使用其他名称重新创建它,现在它显示“[Illuminate\Database\QueryException] SQLSTATE[HY000] [1049] Unknown database 'chatty' (SQL : select * from information_schema.tables where table_schema = chatty and table_name = migrations) [PDOException] SQLSTATE[HY000] [1049] Unknown database 'chatty' " (chatty 是我以前的数据库)
  • 在控制台运行 composer dump-autoload
  • [Illuminate\Database\QueryException] SQLSTATE[42S01]:基表或视图已存在:1050 表“用户”已存在(SQL:创建表 users(`user_id` int unsigned not null , name varchar(255) not null, dob date not null, email varchar(255) not null, `password` varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at time stamp null) 默认字符集 utf8mb4 collat​​e utf8mb4_unicode_ci) [PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
  • 按照你所说的,这就是我得到的
猜你喜欢
  • 2020-12-29
  • 2019-07-17
  • 2014-05-31
  • 1970-01-01
  • 2023-03-22
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
相关资源
最近更新 更多