码迷,mamicode.com
首页 > Web开发 > 详细

Laravel Vuejs 实战:开发知乎 (9)定义话题与问题关系

时间:2020-02-28 14:07:18      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:color   unsigned   etop   this   func   ati   官方文档   href   话题   

1.话题【Topic】

执行命令:

  1 php artisan make:model Topic –cmr


修改****_**_**_create_topics_table.php数据库迁移文件如下:

  1 class CreateTopicsTable extends Migration
  2 {
  3     /**
  4      * Run the migrations.
  5      *
  6      * @return void
  7      */
  8     public function up()
  9     {
 10         Schema::create(‘topics‘, function (Blueprint $table) {
 11             $table->bigIncrements(‘id‘);
 12             $table->string(‘name‘);
 13             $table->text(‘content‘)->nullable();
 14             $table->integer(‘questions_count‘)->default(0);
 15             $table->integer(‘followers_count‘)->default(0);
 16             $table->timestamps();
 17         });
 18     }
 19 
 20     /**
 21      * Reverse the migrations.
 22      *
 23      * @return void
 24      */
 25     public function down()
 26     {
 27         Schema::dropIfExists(‘topics‘);
 28     }
 29 }
 30 

修改Topic.php文件如下:

  1 class Topic extends Model
  2 {
  3     //
  4     protected $fillable = [‘name‘, ‘questions_count‘];
  5 
  6 }
  7 

2.处理话题与问题之间的关联关系【多对多】

单独建一个数据库迁移文件存储话题与问题之间的关系;

创建这个数据库迁移文件,执行命令:

  1 php artisan make:migration create_questions_topics_table --create=questions_topics

修改 ****_create_questions_topics_table.php数据库迁移文件:

  1 class CreateQuestionsTopicsTable extends Migration
  2 {
  3     /**
  4      * Run the migrations.
  5      *
  6      * @return void
  7      */
  8     public function up()
  9     {
 10         Schema::create(‘questions_topics‘, function (Blueprint $table) {
 11             $table->bigIncrements(‘id‘);
 12             $table->bigInteger(‘question_id‘)->unsigned()->index();
 13             $table->bigInteger(‘topic_id‘)->unsigned()->index();
 14             $table->timestamps();
 15         });
 16     }
 17 
 18     /**
 19      * Reverse the migrations.
 20      *
 21      * @return void
 22      */
 23     public function down()
 24     {
 25         Schema::dropIfExists(‘questions_topics‘);
 26     }
 27 }
 28 

执行命令建数据表:

  1 php artisan migrate

3.定义模型Model之间的关联关系:

具体原理及更多介绍看官方文档:模型关联

也有英文版的样例介绍:

larashout网站的:

laravel-eloquent

itsolutionstuff网站的:

Laravel One to One Eloquent Relationship Tutorial

Laravel One to Many Eloquent Relationship Tutorial

Laravel Many to Many Eloquent Relationship Tutorial

Laravel Has Many Through Eloquent Relationship Tutorial

Laravel One to Many Polymorphic Relationship Tutorial

Laravel Many to Many Polymorphic Relationship Tutorial

修改Topic.php文件:

  1 class Topic extends Model
  2 {
  3     //
  4     protected $fillable = [‘name‘, ‘questions_count‘];
  5 
  6 
  7     public function questions()
  8     {
  9         return $this->belongsToMany(
 10             Question::class,
 11             ‘questions_topics‘ //表名我设置的是questions_topics,可能不是系统自动解析的question_topic
 12         )->withTimestamps();//withTimestamps操作questions_topics表中create_at及updated_at字段的;
 13     }
 14 }
 15 

修改Question.php文件:

  1 class Question extends Model
  2 {
  3     //
  4     protected $fillable = [‘title‘, ‘content‘, ‘user_id‘];
  5 
  6     public function topics()
  7     {
  8         return $this->belongsToMany(
  9             Topic::class,
 10             ‘questions_topics‘ //表名我设置的是questions_topics,可能不是系统自动解析的question_topic
 11         )->withTimestamps();//withTimestamps操作questions_topics表中create_at及updated_at字段的
 12     }
 13 }
 14 

话题与问题关系建立完成。

Laravel Vuejs 实战:开发知乎 (9)定义话题与问题关系

标签:color   unsigned   etop   this   func   ati   官方文档   href   话题   

原文地址:https://www.cnblogs.com/dzkjz/p/12376741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!