php - QueryException: 一般错误: 1 no such table: {table_name} (SQL: select * from "{table_name}")
问题描述
- Laravel 版本:5.7
- PHP版本:7.2
- 数据库驱动:sqlite
- 数据库:内存
PHPUnit 上的所有测试都运行良好。但是当我添加View::share
时,AppServiceProvider
所有的 PHPUnit 测试都失败了。
AppServiceProvider.php
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
\View::share('categories', \App\Models\Category::all());
}
}
错误
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: categories (SQL: select * from "categories")
我已经RefreshDatabase
在我的所有测试中使用过。
请指导我,我该如何解决这个问题。我应该在哪里调用View::share
Laravel 应用程序,所以所有测试都应该通过。
解决方案
发生这种情况是因为数据库甚至可能不存在,并且提供程序启动假设数据库/表已启动并正在运行。
通过包裹它来保护它
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
if(\Schema::hasTable('categories'){
\View::share('categories', \App\Models\Category::all());
}
}
}
推荐阅读
- php - Laravel Eloquent orderBy() 一对多关系中的父表字段
- windows - 在 Linux 的 Windows 子系统(Windows 上的 Ubuntu 上的 WSL / Bash)中,如何将颜色重置为默认/原始方案?
- java - 为什么我的地图活动导致我的应用程序崩溃?
- android - 由于只读文件系统,无法在 Android 中打开与 JDBC SQLite 数据库的连接
- spring - @RefreshScope 停止 @Scheduled 任务
- machine-learning - 使您的 SVM 更快、更可靠的最佳方法是什么?
- javascript - 检查 ISO 8601 日期是否早于 5 分钟
- amazon-web-services - 为计算环境设置 EC2 实例标签名称
- android - 将文件从 Firebase 下载到 Android
- php - 在 AJAX 中使用 post 传递文本框数据的正确方法