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)定义话题与问题关系的更多相关文章

  1. Laravel Vuejs 实战:开发知乎 (6)发布问题

    1.view部分: 安装一个扩展包:Laravel-UEditor composer require "overtrue/laravel-ueditor:~1.0" 配置 添加下面 ...

  2. Laravel Vuejs 实战:开发知乎 (10)使用 Select2 优化话题选择

    1.添加选择Topic 使用Select2,如何安装Select2 ,具体使用实例 Select2 and Laravel: Ajax Autocomplete 及 Loading data remo ...

  3. Laravel Vuejs 实战:开发知乎 (2)用户登录

    1.安装一个给用户提示的扩展包: 二选一: https://github.com/laracasts/flash [我选的这个]https://github.com/oanhnn/laravel-fl ...

  4. Laravel Vuejs 实战:开发知乎 (8)美化编辑器

    1.使用UEditor增量包: simple-ueditors 执行下载: git clone https://github.com/JellyBool/simple-ueditor.git 2.用此 ...

  5. Laravel Vuejs 实战:开发知乎 (3)本地化和自定义消息

    1.本地化 由于所有blade默认采用的是 _('')方式输出标签文本,所以可以安装一个语言包,直接指定本地语言为zh_CN即可: 安装 https://github.com/caouecs/Lara ...

  6. Laravel Vuejs 实战:开发知乎 (2)用户注册

    1.本节需要发送验证邮件 2.教程使用SendCloud发送邮件 [我使用的是mailtrap] 3. composer require laravel/ui 安装完成后 php artisan ui ...

  7. Laravel Vuejs 实战:开发知乎 (1)项目环境配置和用户表设计

    1.使用laragon新建laravel项目 zhihu 2.配置env文件的database设置 DB_DATABASE=zhihu 3.分析users表需要的字段 4.修改数据库迁移文件: cla ...

  8. Laravel Vuejs 实战:开发知乎 (5)设计问题表

    1.执行命令: php artisan make:model Models/Question -cm 2.设计问题的数据库迁移文件中的字段: <?php use Illuminate\Datab ...

  9. Laravel Vuejs 实战:开发知乎 (7)验证问题表单字段

    上一节代码中已经实现 下面代码中的validate内部配置就是: public function store(Request $request) { // $data = $request->v ...

随机推荐

  1. Suggestions On Setting LED Holiday Light

    We all like the cheerful glow of holiday lights, so the process goes seamless from start to finish. ...

  2. centos7使用jenkins启动找不到模块

    问题: 在jenkins上启动pycharm项目报:ModuleNotFoundError: No module named 'wanwenyc' 其中‘wanwenyc’为pycharm工程项目路径 ...

  3. JMeter压力测试,http请求压测,5分钟让你学会如何压测接口!

    JMeter压力测试 官网:https://jmeter.apache.org 最新款的jmeter需要java8的支持,所以请自行安装jdk8.这里就不啰嗦了. 可以根据自己的系统下载zip或者是t ...

  4. UI高级组件

    今天继续学习UI高级组件 网格视图 GridView 用GridView标签添加,显示网格视图,需要用到适配器,共有四种适配器 ArrayAdapter,SmipleAdapter,SmipleCou ...

  5. AcWing 906. 区间分组

    //1.将所有区间按左端点从小到大排序 //2.从前往后处理每个区间,判断能否将其放到某个现有的组中 //判断某一组的最后一个区间的右端点是否小于该区间的左端点 //如果大于或等于,就开新组,如果小于 ...

  6. Java:面向对象的编程语言

    java是面向对象的编程语言 Object,就是指面向对象的对象,对象就是实例. 在java里,对象是类的一个具体实例.就像:人,指一个类.你.我.他.张三.李四.王五等则是一个个具体的实例,也就是j ...

  7. find & grep 总 结

    前言 关于本文 总 结 了 find.grep常 规 用 法,正 则 表 达 式,find与 grep合 用 以 及 自 定 义 搜 索 函 数 等 什么是find和grep find 和 grep ...

  8. 题解【loj6277】数列分块入门1

    题目描述 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字\(n\). 第二行输入\(n\)个数字,第\(i\)个数字为\(a_{i}\) ...

  9. Hdu6586 String 字符串字典序贪心

    Hdu6586 字符串字典序贪心 题意 给出一个只包含26个字符的字符串(|S|<=1e5),要求从中取出长度为k的字典序最小的组序列,满足所给的26个字母的限制条件,例如\([l_i,r_i] ...

  10. Django_视图

    1. 视图 1.1 返回json数据 2. url配置 url组成 3. 获取 url参数 别名 4. url反向解析 接收参数 reverse 5. 视图总结 5.1 自定义错误页面 6. Http ...