php - 为什么全局变量在 Laravel 控制器中被重置
问题描述
我正在尝试全局设置一个变量,然后在其中添加值,但它正在被重置,见下文;
class NewTestController extends Controller
{
private $attempted = [];
public function nextQuestion(Request $request) {
$this->attempted[count($this->attempted)] = $request->attempted;
dd($this->attempted);
}
}
如果我使用它本身的功能,它也会被重置。
解决方案
我通过创建一个新数据库解决了我的问题,attempted_questions
因此当尝试提问时,我将其存储到数据库中并检查它是否存在?使用数据库也比使用变量更方便,因为数据库不会像全局变量那样重置。
推荐阅读
- node.js - 异步/等待的问题。不运行下一行
- c++ - 冒泡排序未正确循环
- python - Python:通过 Pandas Dataframe 迭代写入 CSV 文件
- laravel - 使用 dd 时 Laravel Eloquent 结果未提供查看属性的选项
- r - 将一列的每个唯一值与另一列 R 中的每个唯一值相加
- java - 在 Swing 中打印 10 中的数字 1-100
- python-3.x - python中的多个客户端和服务器没有线程
- vb.net - 将整个类标记为线程安全的,从而避免每个成员中的 SyncLock
- c# - 当标签具有值时弹出消息并出现第二种形式
- javascript - 如果对象未定义,则加载组件的最佳方式