0

Im my routes.php I have the following content:

<?php

Route::group(array(), function () {
    View::share('roots', Category::roots()->get());
    $tree = Category::get()->toHierarchy()->toArray();
    View::share('categories', $tree);

    Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'));
});

When my database has no tables yet and I want to do php artisan migrate the result is: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ae_dev.categories' doesn't exist

My migration file:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoriesTable extends Migration {

  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('categories', function(Blueprint $table) {
      $table->increments('id');
      $table->integer('parent_id')->nullable()->index();
      $table->integer('lft')->nullable()->index();
      $table->integer('rgt')->nullable()->index();
      $table->integer('depth')->nullable();
      $table->string('name', 255);

      $table->timestamps();
    });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::drop('categories');
  }

}

I think that Laravel tray to call Category from routes.php and want to do select or somethink so I want to run the migration which creates categories table, but the above error is produced before...

How can I fix this?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Karol F
  • 1,556
  • 2
  • 18
  • 33
  • did you read [this chapter](http://laravel.com/docs/4.2/schema) – worldask Oct 03 '14 at 07:58
  • Seems like you have something that tries to access the database; did you add any commands or services to your artisan? – Repox Oct 03 '14 at 08:07
  • No I didn't add any new command or service to artisan... the problem is not on the migration side I think, but you are right... The Category in routes.php wants access to the database, but why it touches routes.php if all I want to do is run 'php artisan' from command line... – Karol F Oct 03 '14 at 08:45
  • For those who end up being here because of the "migrations" table is not created automatically, this [link](https://stackoverflow.com/a/60095493/10539212) might help. – Phantom1412 Feb 06 '20 at 12:57

1 Answers1

6

It seems that all php artisan commands use routes.php file so when you try in this file to access database tables (and tables don't exist because you haven't run migration) you will get this error. And you cannot run php artisan migrate because you get this error.

One solution is to remove code that queries database but it's of course not good solution. So what you should do is to choose table from first migration you ever do (in your case it's probably categories. Later you have more migrations but this will be first) and add something like that:

if (!Schema::hasTable('categories'))
{
    return;
}

into your routes.php file.

However if you will play more with migrations and will need also other tables to queries you will need to change the above condition for example into:

if (!Schema::hasTable('categories') || !Schema::hasTable('users'))
{
    return;
}

but it will still cause some problems - you rather don't want to run this code each time in your routes, so I would do it this way:

if ($env == 'local') {

    if (!Schema::hasTable('categories') || !Schema::hasTable('users'))
    {
        return;
    }
}

You need to configure your environment of course. But now you run this code only for local environment, on production this code won't be run so it won't affect app performance. Of course I assume here that you won't play with artisan on production.

EDIT

But in case you use queries only for sharing data (and not for routes), I would move those lines:

View::share('roots', Category::roots()->get());
$tree = Category::get()->toHierarchy()->toArray();
View::share('categories', $tree);

to BaseController and run method (or parent constructor) in all Controllers that extend it.

Old answer (not adequate in this case)

The error information is clear enough with your explanation that you have no tables in your database yet. You cannot run database queries if you have no tables.

In your migration you should create necessary tables for example this is migration for user table:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class NewUser extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function ($table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('user_name', 60)->unique();
            $table->string('email', 120)->unique();
            $table->string('passwd', 256);
            $table->decimal('balance', 8, 2);
            $table->rememberToken();
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
        Schema::drop('users');
    }

}

You should also not insert data into database when using migration (as you probably do now). You should do it while seeding tables.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291