Framework: QueryException: General error: 1 no such table: {table_name} (SQL: select * from "{table_name}")

Created on 31 Dec 2018  路  4Comments  路  Source: laravel/framework

  • Laravel Version: 5.7
  • PHP Version: 7.2
  • Database Driver: sqlite
  • Database: memory

All testing on PHPUnit was working completely fine. But when I added View::share in AppServiceProvider then all the PHPUnit tests failed.

AppServiceProvider.php

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        \View::share('categories', \App\Models\Category::all());
    }
}

Error

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: categories (SQL: select * from "categories")

Please guide me, how can I fix this. Where should I call View::share in Laravel app so, all tests should pass.

Most helpful comment

https://laravel.com/docs/5.7/views#view-composers. If you use '*' for the view, you can ensure your categories variable is available in all views.

All 4 comments

It doesn't work because AppServiceProvider::boot() is executed before the database is migrated.

@staudenmeir I think you might have misunderstood the question. The way I understood it, is that @ellis22 doesn't want to move that code to only be in the tests. The solution here will be to use Laravel's view composer feature I think. :)

https://laravel.com/docs/5.7/views#view-composers. If you use '*' for the view, you can ensure your categories variable is available in all views.

@GrahamCampbell you are right I want to share categories variable in all views. I tried view composer '*' also. But getting same PHPunit error.

Edit: view composer '*' is working. I run the phpunit test before saving AppServiceProvider.
Thank you :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RomainSauvaire picture RomainSauvaire  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

YannPl picture YannPl  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

jackmu95 picture jackmu95  路  3Comments