- 创建回答Migration
- 创建回答表结构
- 创建回答表模型
- 实现回答表操作
1. 创建回答Migration
php artisan make:migration create-table-answers --create=answers
# Lavarel 5.4
php artisan make:migration create_answers_table --create=answers
2. 创建回答表结构
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTableAnswers extends Migration
{
public function up()
{
Schema::create('answers', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
//自定义字段
$table->text('content')->comment('回答内容');
$table->unsignedInteger('user_id')->comment('回答用户');
$table->unsignedInteger('question_id')->comment('问题编号');
//外键连接
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('question_id')->references('id')->on('questions');
});
}
public function down()
{
Schema::dropIfExists('answers');
}
}
php artisan migrate --pretend
php artisan migrate
php artisan migrate:rollback
3. 创建回答表模型
php artisan make:model Model\Answer
MAC Laravel5.4
php artian make:model Models\\Answer
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Answer extends Model
{
}
4. 实现回答表操作
路由文件中提取公共函数
function answerInstance(){
return new App\Model\Answer;
}
# 版本2
use App\Models\Answer;
function answer(){
return new Answer();
}
4.1 添加回答API
创建路由
Route::any('/api/answer/add',function(){
return answerInstance()->add();
});
实现方法
/*添加回答API*/
public function add()
{
//检查用户是否登录
if(!userInstance()->isLogin()){
return ['err'=>1, 'msg'=>'请先登录'];
}
//检查问题是否存在
if(!(rq('question_id') && rq('content'))){
return ['err'=>1, 'msg'=>'参数错误'];
}
//检查问题是否存在
$question = questionInstance()->find(rq('question_id'));
if(!$question){
return ['err'=>1, 'msg'=>'问题不存在'];
}
//相同问题进行回答一次
$count = $this->where(['question_id'=>rq('question_id'), 'user_id'=>session('user.id')])->count();
if($count){
return ['err'=>1, 'msg'=>'您已回答'];
}
//添加数据库
$this->user_id = session('user.id');
$this->question_id = rq('question_id');
$this->content = rq('content');
return $this->save() ? ['err'=>0,'id'=>$this->id] : ['err'=>1,'msg'=>'添加失败'];
}
# 版本2
/*API 添加回答*/
public function add()
{
//判断用户是否登录
if(!user()->islogin()){
return ['err'=>1, 'msg'=>'尚未登录'];
}
$this->user_id = session('user_id');
//判断问题和回答
if(!rq('question_id') || !rq('content')){
return ['err'=>1, 'msg'=>'缺少问题编号和回答内容'];
}
//判断问题是否存在
$question = question()->find(rq('question_id'));
if(!$question){
return ['err'=>1, 'msg'=>'问题不存在'];
}
$this->question_id = $question->id;
//同一问题仅作答一次
$count = $this->where(['question_id'=>rq('question_id'), 'user_id'=>session('user_id')])->count();
if($count){
return ['err'=>1, 'msg'=>'已回答过'];
}
//数据库添加
$this->content=rq('content');
return $this->save() ? ['err'=>0,'msg'=>'回答成功','id'=>$this->id] : ['err'=>0,'msg'=>'回答失败'];
}
4.2 更新回答API
创建路由
Route::any('/api/answer/edit',function(){
return answerInstance()->edit();
});
实现方法
/*更新回答API*/
public function edit()
{
//检查用户是否登录
if(!userInstance()->isLogin()){
return ['err'=>1, 'msg'=>'请先登录'];
}
//检查答案是否存在
if(!(rq('id') && rq('content')) ){
return ['err'=>1, 'msg'=>'参数错误'];
}
//检查当答案的拥有者是否为当前用户
$answer = $this->find(rq('id'));
if($answer->user_id != session('user.id')){
return ['err'=>1, 'msg'=>'权限不足'];
}
//更新答案
if(rq('content')){
$answer->content = rq('content');
}
return $answer->save() ? ['err'=>0] : ['err'=>1,'msg'=>'更新失败'];
}
# 版本2
/*API 编辑回答*/
public function edit()
{
//判断用户是否登录
if(!user()->islogin()){
return ['err'=>1, 'msg'=>'尚未登录'];
}
//判断回答编号是否存在
if(!rq('id') || !rq('content')){
return ['err'=>1, 'msg'=>'缺少回答编号和回答内容'];
}
//判断回答是否存在
$answer = answer()->find(rq('id'));
if(!$answer){
return ['err'=>1, 'msg'=>'回答不存在'];
}
//作答人可编辑
if($answer['user_id'] != session('user_id')){
return ['err'=>1, 'msg'=>'权限不足'];
}
//数据库更新
$answer->content=rq('content');
return $answer->save() ? ['err'=>0,'msg'=>'编辑成功'] : ['err'=>0,'msg'=>'编辑失败'];
}
4.3 查看回答API
创建路由
Route::any('/api/answer/read',function(){
return answerInstance()->read();
});
实现方法
/*查看回答API*/
public function read()
{
//当任意一个参数不存在时均报错
if(!rq('id') && !rq('question_id')){
return ['err'=>1, 'msg'=>'参数错误'];
}
//根据回答编号查看回答
if(rq('id')){
$data = $this->find(rq('id'));
if(!$data){
return ['err'=>1, 'msg'=>'暂无数据'];
}
return ['err'=>0, 'data'=>$data];
}
//根据问题编号查看回答
if(rq('question_id')){
$data = $this->where('question_id',rq('question_id'))->get(['id','content'])->keyBy('id');
if(!$data){
return ['err'=>1, 'msg'=>'暂无数据'];
}
return ['err'=>0, 'data'=>$data];
}
}
# 版本2
/*API 查看回答*/
public function read()
{
if(!rq('id') && !rq('question_id')){
return ['err'=>1, 'msg'=>'缺少回答编号或问题编号'];
}
//查看单条回答
if(rq('id')){
$answer = $this->find(rq('id'));
if(!$answer){
return ['err'=>1,'msg'=>'回答不存在'];
}
return ['err'=>0,'data'=>$answer];
}
//判断问题是否存在
$question = question()->find(rq('question_id'));
if(!$question){
return ['err'=>1, 'msg'=>'问题不存在'];
}
//查看指定问题下的回答
$answers = $this->where('question_id',rq('question_id'))->get()->keyBy('id');
return ['err'=>0,'data'=>$answers];
}