LaravelDaily / Laravel-Reddit-Clone-Course

21 stars 6 forks source link

On fresh install php artisan migrate fails due to code in AppServiceProvider.php #1

Open crazedVic opened 3 years ago

crazedVic commented 3 years ago
public function boot()
    {
        Paginator::useBootstrap();

        View::share('newestPosts', Post::with('community')->latest()->take(5)->get());
        View::share('newestCommunities', Community::withCount('posts')->latest()->take(5)->get());

        PostVote::observe(PostVoteObserver::class);
    }

The View::share commands fail because the db on a fresh install is still empty. Migration can't run because this code will run first. This is what happens:

php artisan migrate

   Illuminate\Database\QueryException

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'reddit.posts' doesn't exist (SQL: select * from `posts` where `posts`.`deleted_at` is null order by `created_at` desc limit 5)

  at C:\Users\User\projects\php\Laravel-Reddit-Clone-Course\vendor\laravel\framework\src\Illuminate\Database\Connection.php:678
    674▕         // If an exception occurs when attempting to run a query, we'll format the error
    675▕         // message to include the bindings with SQL, which will make this exception a
    676▕         // lot more helpful to the developer instead of just the database's errors.
    677▕         catch (Exception $e) {
  ➜ 678▕             throw new QueryException(
    679▕                 $query, $this->prepareBindings($bindings), $e
    680▕             );
    681▕         }
    682▕

  • A table was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`.
    https://laravel.com/docs/master/migrations#running-migrations

  1   [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider))

  2   C:\Users\User\projects\php\Laravel-Reddit-Clone-Course\vendor\laravel\framework\src\Illuminate\Database\Connection.php:338
      PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'reddit.posts' doesn't exist")
Satal commented 2 years ago

@crazedVic did you end up resolving this?

crazedVic commented 2 years ago

@Satal I think I just commented out the code when running the migrations.

Satal commented 2 years ago

@Satal I think I just commented out the code when running the migrations.

Thanks @crazedVic