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.
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 :)
Most helpful comment
https://laravel.com/docs/5.7/views#view-composers. If you use
'*'for the view, you can ensure yourcategoriesvariable is available in all views.