如何在laravel中连接'users'table和'post'table?
How to connect 'users' table with 'post' table in laravel?
这是我的结构,我想在laravel中连接这两个表,怎么办?
Post Table:
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();
});
}
用户Table:
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();
});
}
在你的帖子中 table 你应该有:
$table->integer('user_id')->unsigned();
在您的用户模型上:
public function posts(){
return $this->hasMany(Post::class);
}
在您的 Post 型号上:
public function user(){
return $this->belongsTo(User::class);
}
我假设您只是在迁移中粘贴,但您确实需要在 您的 posts
[=] 之前创建 users
table 33=]。我会改变
$table->foreign('id_fk')->references('id')->on('users');
到
$table->foreign('user_id')->references('id')->on('users');
因为Laravel可以推断出外键:
Eloquent determines the default foreign key name by examining the name
of the relationship method and suffixing the method name with _id.
However, if the foreign key on the Post model is not user_id, you
may pass a custom key name as the second argument to the belongsTo
method
然后您在模型中所需的全部内容如下:
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。
这是我的结构,我想在laravel中连接这两个表,怎么办?
Post Table:
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();
});
}
用户Table:
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();
});
}
在你的帖子中 table 你应该有:
$table->integer('user_id')->unsigned();
在您的用户模型上:
public function posts(){
return $this->hasMany(Post::class);
}
在您的 Post 型号上:
public function user(){
return $this->belongsTo(User::class);
}
我假设您只是在迁移中粘贴,但您确实需要在 您的 posts
[=] 之前创建 users
table 33=]。我会改变
$table->foreign('id_fk')->references('id')->on('users');
到
$table->foreign('user_id')->references('id')->on('users');
因为Laravel可以推断出外键:
Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id. However, if the foreign key on the Post model is not user_id, you may pass a custom key name as the second argument to the belongsTo method
然后您在模型中所需的全部内容如下:
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。