一、创建模型
创建评论模型
php artisan make:model Comment -m
修改生成的 database/migrations/create_comments_table.php
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->text('content');
$table->unsignedInteger('commentable_id');
$table->string('commentable_type');
$table->timestamps();
});
}
执行数据迁移
php artisan migrate
修改 app/Comment.php
protected $table = 'comments';
protected $fillable = ['user_id','content','commentable_id','commentable_type'];
public function commentable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
修改 app/Answer.php
public function comments()
{
return $this->morphMany('App\Comment','commentable');
}
修改 app/Question.php
public function comments()
{
return $this->morphMany('App\Comment','commentable');
}
二、创建控制器
创建评论控制器
php artisan make:controller CommentsController
修改 routes/api.php
Route::post('/comments', 'CommentsController@index');
Route::post('/comment/store', 'CommentsController@store')->middleware('auth:api');
修改 app/Repositories/QuestionRepository.php
public function getQuestionCommentsById($id)
{
$question = Question::with('comments','comments.user')->where('id',$id)->first();
return $question->comments;
}
修改 app/Repositories/AnswerRepository.php
public function getAnswerCommentsById($id)
{
$answer = Answer::with('comments','comments.user')->where('id',$id)->first();
return $answer->comments;
}
新建 app/Repositories/CommentRepository.php
namespace App\Repositories;
use App\Comment;
class CommentRepository
{
public function create(array $attributes)
{
return Comment::create($attributes);
}
}
修改生成的控制器:app/Http/Controllers/CommentsController.php
namespace App\Http\Controllers;
use App\Repositories\AnswerRepository;
use App\Repositories\CommentRepository;
use App\Repositories\QuestionRepository;
use Auth;
class CommentsController extends Controller
{
private $answerRepository;
private $commentRepository;
private $questionRepository;
public function __construct(AnswerRepository $answerRepository, CommentRepository $commentRepository, QuestionRepository $questionRepository)
{
$this->answerRepository = $answerRepository;
$this->commentRepository = $commentRepository;
$this->questionRepository = $questionRepository;
}
public function index()
{
if(request('type') == 'question'){
return $this->questionRepository->getQuestionCommentsById(request('model'));
}
return $this->answerRepository->getAnswerCommentsById(request('model'));
}
public function store()
{
$model = $this->getModelNameFormType(request('type'));
return $this->commentRepository->create([
'commentable_id' => request('model'),
'commentable_type' => $model,
'user_id' => user('api')->id,
'content' => request('content'),
]);
}
private function getModelNameFormType($type)
{
return $type === 'question' ? 'App\Question' : 'App\Answer';
}
}
三、创建视图
修改 resources/views/layouts/app.blade.php
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>;
Laravel.apiToken = "{{ Auth::check() ? 'Bearer '.Auth::user()->api_token : ''}}"
@if(Auth::check())
window.Zhihu = {
name:"{{Auth::user()->name}}",
avatar:"{{Auth::user()->avatar}}"
}
@endif
新建 resources/assets/js/components/comments.vue
注册组件:修改 resources/assets/js/app.js
Vue.component('comments', require('./components/Comments.vue'));
执行 gulp
gulp
修改 resources/views/questions/show.blade.php
访问:http://127.0.0.1:8000/questions/id,id 为问题 ID
点击按钮进行测试