7.1. 回复数据
回复列表
目前我们已经有话题发布功能,接下来我们一起开发话题回复功能,允许用户参与讨论某个话题。回复功能包括:
- 回复列表
- 发表回复
- 删除回复
- 消息通知
本章节,我们先开发『回复列表』功能。
代码生成
使用代码生成器快速构建骨架代码:
$ php artisan make:scaffold Reply --schema="topic_id:integer:unsigned:default(0):index,user_id:integer:unsigned:default(0):index,content:text"
数据模型
修改下 Reply 模型的 $fillable
属性,我们只允许用户更改 content
字段。同时做下数据模型的关联,一条回复属于一个话题,一个条回复属于一个作者所有:
app/Models/Reply.php
<?php
namespace App\Models;
class Reply extends Model
{
protected $fillable = ['content'];
public function topic()
{
return $this->belongsTo(Topic::class);
}
public function user()
{
retu...