The server of this project is developed using Laravel 5.8 framework.
Local environment Uses the LNMP environment deployed by Docker
directory
composer
Build the project- Create tables and data models
- Populate test data
Create a project
After selecting a working directory, run the composer create-project –prefer-dist laravel/laravel ant-qa-server 5.8.* command
Create a new project, ant-QA-Server
Configuring the Database
Example Modify the. Env file
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Copy the code
Change to your own database configuration information
buildmigration
Migrate files and data models
Migrate document reference addresses
The newly created Laravel project, which will automatically create the migration files for the Users and PASSword_resets tables, also needs to be created for this project
questions
Table for storing problem recordslike_questions
Table, store user like question recordsanswer_question
Table, where the user answers the question record
Create migration files
Problem sheet
Run the artisan command PHP artisan make:model Models/ question-m
Create a migration file in database/ Migrations and edit it as follows
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateQuestionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('questions'.function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title')->comment('Question title');
$table->text('content')->comment('Content content');
$table->bigInteger('user_id')->comment('Creator user ID');
$table->integer('comment_num')->default(0)->comment('Number of comments');
$table->integer('like_num')->default(0)->comment('Likes');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('questions'); }}Copy the code
At the same time, a corresponding model will be created, located in app/Models/, Models directory does not exist, will be created automatically, edit as follows
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Question extends Model
{
use SoftDeletes;
protected $fillable = ['title'.'user_id'.'content'.'comment_num'.'like_num'];
public function answers()
{
return $this->hasMany(AnswerQuestion::class); }}Copy the code
The ANSWERS method defines a one-to-many relationship between questions and answers
Like problem sheet
php artisan make:model Models/LikeQuestion -m
Edit the migration file database/migrations/*_create_like_questions_table_*
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLikeQuestionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('like_questions'.function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->comment('User ID like people');
$table->bigInteger('question_id')->comment('Favored question ID');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('like_questions'); }}Copy the code
Edit app/Models/LikeQuestion model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class LikeQuestion extends Model
{
use SoftDeletes;
protected $fillable = ['user_id'.'question_id'];
}
Copy the code
Answer question sheet
php artisan make:model Models/AnswerQuestion -m
Edit the migration file database/migrations/* _create_answer_QUESTIONs_table_ *
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAnswerQuestions extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('answer_questions'.function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->comment('User ID like people');
$table->bigInteger('question_id')->comment('Favored question ID');
$table->text('content')->comment('Content of the answer');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('answer_questions'); }}Copy the code
Edit the app/Models/AnswerQuestion model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class AnswerQuestion extends Model
{
use SoftDeletes;
protected $fillable = ['user_id'.'question_id'.'content'];
}
Copy the code
Run the artisan command in the project root directory PHP artisan migrate to migrate all unmigrated files in the application.