Laravel Vuejs 实战:开发知乎 (16)创建问题的答案 Answer

1.执行命令

  1 php artisan make:model Answer –cmrf 

一次性把model,及对应的controller migration factory 创建好,并且controller配置设置为resource

2.修改****_**_**_******_create_answers_table.php:

  1 <?php
  2 
  3 use Illuminate\Database\Migrations\Migration;
  4 use Illuminate\Database\Schema\Blueprint;
  5 use Illuminate\Support\Facades\Schema;
  6 
  7 class CreateAnswersTable extends Migration
  8 {
  9     /**
 10      * Run the migrations.
 11      *
 12      * @return void
 13      */
 14     public function up()
 15     {
 16         Schema::create('answers', function (Blueprint $table) {
 17             $table->bigIncrements('id');
 18             $table->unsignedBigInteger('user_id')->index()->comment("答案是哪个用户创建的");
 19             $table->unsignedBigInteger('question_id')->index()->comment("答案是回答的哪个问题");
 20             $table->text('content')->comment("答案的具体内容");
 21             $table->integer('votes_count')->default(0)->comment("点赞总数");
 22             $table->integer('comments_count')->default(0)->comment("回复总数");
 23             $table->string('is_hidden', 8)->default("F")->comment("回答状态是否被隐藏,默认设置长度必须大于8");//默认设置长度必须大于8,否则不被赋值F,即被隐藏
 24             $table->string('close_comment')->default('F')->comment("回答是否被关闭评论");//默认F代表否,即不关闭,如果需要关闭设置为 T
 25             $table->softDeletes()->comment("支持软删除");
 26             $table->timestamps();
 27         });
 28     }
 29 
 30     /**
 31      * Reverse the migrations.
 32      *
 33      * @return void
 34      */
 35     public function down()
 36     {
 37         Schema::dropIfExists('answers');
 38     }
 39 }
2020_02_29_123742_create_answers_table.php

运行数据库迁移,执行:

  1 php artisan migrate

对Answer 模型添加

  1 protected $fillable = ['user_id', 'question_id', 'content'];

防止 massAssignmentException

3. 模型关联关系:

Answer,Question,User 之间的模型关联关系需要在各自Model类文件定义:

  1 <?php
  2 
  3 namespace App;
  4 
  5 use App\Models\Question;
  6 use Illuminate\Database\Eloquent\Model;
  7 use Illuminate\Database\Eloquent\SoftDeletes;
  8 
  9 class Answer extends Model
 10 {
 11     #region 支持软删除添加
 12     use SoftDeletes;
 13     protected $dates = ['deleted_at'];
 14 
 15     #endregion
 16 
 17     protected $fillable = ['user_id', 'question_id', 'content'];
 18 
 19     /** 一个回答只有一个回答主人
 20      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 21      */
 22     public function user()
 23     {
 24         return $this->belongsTo(User::class);
 25     }
 26 
 27     /** 一个回答只针对一个问题
 28      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 29      */
 30     public function question()
 31     {
 32         return $this->belongsTo(Question::class);
 33     }
 34 
 35 }
 36 
Answer.php

  1 <?php
  2 
  3 namespace App\Models;
  4 
  5 use App\Answer;
  6 use App\Topic;
  7 use App\User;
  8 use Illuminate\Database\Eloquent\Model;
  9 use Illuminate\Database\Eloquent\SoftDeletes;
 10 
 11 class Question extends Model
 12 {
 13     //软删除 添加
 14     use SoftDeletes;
 15     //
 16     protected $fillable = ['title', 'content', 'user_id'];
 17     //支持软删除 添加
 18     protected $dates = ['deleted_at'];
 19 
 20     public function topics()
 21     {
 22         return $this->belongsToMany(
 23             Topic::class,
 24             'questions_topics' //表名我设置的是questions_topics,可能不是系统自动解析的question_topic
 25         )->withTimestamps();//withTimestamps操作questions_topics表中create_at及updated_at字段的
 26     }
 27 
 28     public function user()
 29     {
 30         return $this->belongsTo(User::class);
 31     }
 32 
 33     /** scope+请求名命名的
 34      * @return bool
 35      */
 36     public function scopePublished($query)
 37     {
 38         return $query->where('is_hidden', 'F');//等于F表示不隐藏
 39     }
 40 
 41 
 42     /** 一个问题有多个回答
 43      * @return \Illuminate\Database\Eloquent\Relations\HasMany
 44      */
 45     public function answers()
 46     {
 47         return $this->hasMany(Answer::class);
 48     }
 49 
 50 }
 51 
Question.php
  1 <?php
  2 
  3 namespace App;
  4 
  5 use App\Models\Question;
  6 use Illuminate\Contracts\Auth\MustVerifyEmail;
  7 use Illuminate\Foundation\Auth\User as Authenticatable;
  8 use Illuminate\Notifications\Notifiable;
  9 
 10 class User extends Authenticatable implements MustVerifyEmail
 11 {
 12     use Notifiable;
 13 
 14     /**
 15      * The attributes that are mass assignable.
 16      *
 17      * @var array
 18      */
 19     protected $fillable = [
 20         'name', 'email', 'password', 'avatar', 'activation_token'
 21     ];
 22 
 23     /**
 24      * The attributes that should be hidden for arrays.
 25      *
 26      * @var array
 27      */
 28     protected $hidden = [
 29         'password', 'remember_token',
 30     ];
 31 
 32     /**
 33      * The attributes that should be cast to native types.
 34      *
 35      * @var array
 36      */
 37     protected $casts = [
 38         'email_verified_at' => 'datetime',
 39     ];
 40 
 41 
 42     /**添加用户模型和问题模型的模型关联
 43      * @return \Illuminate\Database\Eloquent\Relations\HasMany
 44      */
 45     public function questions()
 46     {
 47         return $this->hasMany(Question::class);
 48     }
 49 
 50 
 51     /** 添加用户模型和回答模型的模型关联 一个用户可以有多个回答
 52      * @return \Illuminate\Database\Eloquent\Relations\HasMany
 53      */
 54     public function answers()
 55     {
 56         return $this->hasMany(Answer::class);
 57     }
 58 }
 59 
 60 
User.php

补充Question,User 各自数据表中缺少的字段:

注意:Question迁移是否需要处理foreign key onDelete,问题被删除的时候,回答也会删除?

不删除,因为答主应该还可以查看,等于把问题页面隐藏了,这样外界没有从问题查看回答内容的入口,但是答主自己可以看

执行:

  1 php artisan make:migration add_answer_id_to_users_table --table=users

修改 ****_add_answer_id_to_users_table.php

注:补充一下让用户也支持软删除:User.php中记得添加:

  1 #region 支持软删除
  2 use SoftDeletes;
  3 protected $dates = ['deleted_at'];
  4 #endregion
  5 

User.php:

  1 <?php
  2 
  3 namespace App;
  4 
  5 use App\Models\Question;
  6 use Illuminate\Contracts\Auth\MustVerifyEmail;
  7 use Illuminate\Database\Eloquent\SoftDeletes;
  8 use Illuminate\Foundation\Auth\User as Authenticatable;
  9 use Illuminate\Notifications\Notifiable;
 10 
 11 class User extends Authenticatable implements MustVerifyEmail
 12 {
 13     use Notifiable;
 14     #region 支持软删除
 15     use SoftDeletes;
 16     protected $dates = ['deleted_at'];
 17     #endregion
 18     /**
 19      * The attributes that are mass assignable.
 20      *
 21      * @var array
 22      */
 23     protected $fillable = [
 24         'name', 'email', 'password', 'avatar', 'activation_token'
 25     ];
 26 
 27     /**
 28      * The attributes that should be hidden for arrays.
 29      *
 30      * @var array
 31      */
 32     protected $hidden = [
 33         'password', 'remember_token',
 34     ];
 35 
 36     /**
 37      * The attributes that should be cast to native types.
 38      *
 39      * @var array
 40      */
 41     protected $casts = [
 42         'email_verified_at' => 'datetime',
 43     ];
 44 
 45 
 46     /**添加用户模型和问题模型的模型关联
 47      * @return \Illuminate\Database\Eloquent\Relations\HasMany
 48      */
 49     public function questions()
 50     {
 51         return $this->hasMany(Question::class);
 52     }
 53 
 54 
 55     /** 添加用户模型和回答模型的模型关联 一个用户可以有多个回答
 56      * @return \Illuminate\Database\Eloquent\Relations\HasMany
 57      */
 58     public function answers()
 59     {
 60         return $this->hasMany(Answer::class);
 61     }
 62 }
 63 
 64 
User.php


  1 <?php
  2 
  3 use Illuminate\Database\Migrations\Migration;
  4 use Illuminate\Database\Schema\Blueprint;
  5 use Illuminate\Support\Facades\Schema;
  6 
  7 class AddAnswerIdToUsersTable extends Migration
  8 {
  9     /**
 10      * Run the migrations.
 11      *
 12      * @return void
 13      */
 14     public function up()
 15     {
 16         Schema::table('users', function (Blueprint $table) {
 17             //
 18             $table->unsignedBigInteger('answer_id')->nullable()->comment("用户回答的答案id");//用户可以没有回答过任何问题
 19             //补一下 支持软删除
 20             $table->softDeletes();
 21         });
 22     }
 23 
 24     /**
 25      * Reverse the migrations.
 26      *
 27      * @return void
 28      */
 29     public function down()
 30     {
 31         Schema::table('users', function (Blueprint $table) {
 32             //
 33             $table->dropColumn('answer_id');
 34         });
 35     }
 36 }
 37 
2020_02_29_125944_add_answer_id_to_users_table.php

执行:

  1 php artisan make:migration add_answer_id_to_questions_table --table=questions

修改:****_add_answer_id_to_questions_table.php

  1 <?php
  2 
  3 use Illuminate\Database\Migrations\Migration;
  4 use Illuminate\Database\Schema\Blueprint;
  5 use Illuminate\Support\Facades\Schema;
  6 
  7 class AddAnswerIdToQuestionsTable extends Migration
  8 {
  9     /**
 10      * Run the migrations.
 11      *
 12      * @return void
 13      */
 14     public function up()
 15     {
 16         Schema::table('questions', function (Blueprint $table) {
 17             //
 18             $table->unsignedBigInteger('answer_id')->nullable()->comment("问题下回答的答案id");//问题可以没有任何回答
 19         });
 20     }
 21 
 22     /**
 23      * Reverse the migrations.
 24      *
 25      * @return void
 26      */
 27     public function down()
 28     {
 29         Schema::table('questions', function (Blueprint $table) {
 30             //
 31             $table->dropColumn('answer_id');
 32         });
 33     }
 34 }
2020_02_29_130231_add_answer_id_to_questions_table.php

执行:

  1 php artisan migrate

完成数据库迁移文件的运行。

posted @ 2020-02-29 20:39  dzkjz  阅读(242)  评论(0编辑  收藏  举报