由于 Laravel 5.3 更新,当我更新数据库中的数据时,我的视图没有正确刷新

Since Laravel 5.3 update, my views are not refreshed properly when I update data in my DB

这里有一个非常奇怪的问题。我有一个完美运行的 Laravel 5.2 应用程序。然后我更新到 Laravel 5.3 以使用新的广播功能,但我遇到了一个大问题。

当我更新数据(使用我的申请表或直接在我的数据库中)时,视图会被正确更新。我尝试清除缓存、视图和配置,但没有任何改变...我需要转到其他一些页面,数据在出现时完成...

我有一个 Campaign 模型和一个列出活动的页面。当我直接在数据库中删除一个条目时,前面的列表不会改变。另外,当我使用像 dd 这样的调试功能时,结果告诉我数据没有改变...

有没有其他人遇到同样的问题?

我已经按照迁移指南将我的 5.2 更新到 5.3,也许我忘记了什么...

这是我的 .env 文件的一部分:

DB_CONNECTION=mysql
BROADCAST_DRIVER=redis
CACHE_DRIVER=array
SESSION_DRIVER=file
QUEUE_DRIVER=database

谢谢!

感谢您分享这个问题。

Laravel 成功升级到 5.3 版本,有一些弃用和应用程序服务提供商,还添加了一些新功能,如护照。

你的问题出在视图上。据我所知,您需要从 "boot" 方法中删除在 EventServiceProviderRouteServiceProviderAuthServiceProvider 中编写的参数,这些参数在 app/provider/remove_the_arguments_from_boot_method_given_file

在Laravel 5.2中:

 public function boot(GateContract $gate)
    {
        $this->registerPolicies($gate);
    }

但在 Laravel 5.3:

 public function boot()
    {
        parent::boot();

    }

请参考Laravel 5.3 docs

我希望这对你有用。

感谢和问候。

切勿在会话中存储完整模型,这会导致应用程序中显示旧数据!

经过一天的搜索和重构,我发现了原来的问题所在!

这是一个简单的 session() 语句,导致我的应用程序显示无效数据。

历史

仪表板显示链接到客户的活动列表。一个用户可以管理多个客户端,因此我将当前客户端置于会话中以了解当前使用的是哪个客户端。

这里的错误是我将整个客户端模型放在会话中,所以当我读取会话和检索数据时,所有关系也被检索。

客户端是我的应用程序中访问数据的中心点。我检索链接到我的客户的活动,一切都与之相关。

这里是恶性函数:

/**
 * Retrieve the current client instance when the user is connected
 * @return App\Client|null
 */
protected function retrieveCurrentClient()
{
    $client = null;

    if (Gate::allows('manage-clients')) {
        if (null === $client = session('currentClient')) {
            $client = Client::all()->first();
            session(['currentClient' => $client]);
        }
    } elseif (Auth::guard()->check()) {
        $client = Auth::guard()->user()->client;
    }

    return $client;
}

事实上,当我深入研究 Gate 定义时,问题就出现了。如果我删除它们,我的应用程序将再次开始工作...

解决方案

我只是将函数更改为存储在会话客户端 ID 而不是完整模型中。然后我在我的应用程序的每个页面中检索新数据。

/**
 * Retrieve the current client instance when the user is connected
 * @return App\Client|null
 */
protected function retrieveCurrentClient()
{
    $client = null;

    if (Gate::allows('manage-clients')) {
        if (null === $client_id = session('client_id')) {
            $client = Client::all()->first();
            session(['client_id' => $client->id]);
        } else {
            $client = Client::findOrFail($client_id);
        }
    } elseif (Auth::guard()->check()) {
        $client = Auth::guard()->user()->client;
    }

    return $client;
}

不知道它是否可以帮助其他人避免这些错误,但很高兴找到答案!