用模型、迁移与关联查询操作数据库。

本节要点

  • 每个模型对应一张表
  • make:model -m 同时生成迁移
  • hasMany/belongsTo 定义关联

代码示例

class Post extends Model
{
    protected $fillable = ['title', 'body'];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}
提示:批量赋值前通过 fillable 或 guarded 限定字段。