首页 > 解决方案 > 为什么全局变量在 Laravel 控制器中被重置

问题描述

我正在尝试全局设置一个变量,然后在其中添加值,但它正在被重置,见下文;

class NewTestController extends Controller
{
    private $attempted = [];
    public function nextQuestion(Request $request) {
        $this->attempted[count($this->attempted)] = $request->attempted;
        dd($this->attempted);
    }
}

如果我使用它本身的功能,它也会被重置。

标签: phplaravelglobal-variables

解决方案


我通过创建一个新数据库解决了我的问题,attempted_questions因此当尝试提问时,我将其存储到数据库中并检查它是否存在?使用数据库也比使用变量更方便,因为数据库不会像全局变量那样重置。


推荐阅读