kkhicher1's avatar

View::share('cities', City::all()) return cities table missing with feature test

Hi,

When i use View::share('cities', City::all()) in AppServiceProvider.php and try to test with phpunit i got error of cities table missing.

AppServiceProvider.php

<?php

namespace App\Providers;

use App\Models\City;
use App\Models\Operator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('cities', City::all());
        View::share('operators', Operator::all());
    }
}
0 likes
3 replies
tykus's avatar

You could also check that the tables exist using:

if (\Schema::hasTable('cities')) {
    View::share('cities', City::all());
}
if (\Schema::hasTable('operators')) {
    View::share('operators', Operator::all());
}

This gives the test suite the opportunity to migrate the database without error(s); and the table(s) will then exist for any test examples.

Please or to participate in this conversation.