23
votes

I'm trying to make following tutorial: https://medium.com/on-coding/e8d93c9ce0e2

When it comes to run:

php artisan migrate

I get following error:

[Exception]                                                                        
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.user' doesn't   
exist (SQL: alter table `user` add `id` int unsigned not null auto_increment prim  
ary key, add `username` varchar(255) null, add `password` varchar(255) null, add   
`email` varchar(255) null, add `created_at` datetime null, add `updated_at` datet  
ime null) (Bindings: array (                                                       
))

Database connection is working, the migrations table was created successfully. Database name was changed as you can see in the error message.

Whats quite strange to me, is that it tries to alter the table - which doesn't exists - and not to create it.

Here are my other files, like UserModel, Seeder, Migtation and DB Config.

CreateUserTable:

<?php

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

class CreateUserTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('user', function(Blueprint $table)
    {

        $table->increments("id");

        $table
            ->string("username")
            ->nullable()
            ->default(null);
        $table
            ->string("password")
            ->nullable()
            ->default(null);
        $table
            ->string("email")
            ->nullable()
            ->default(null);
        $table
            ->dateTime("created_at")
            ->nullable()
            ->default(null);
        $table
            ->dateTime("updated_at")
            ->nullable()
            ->default(null);

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('user', function(Blueprint $table)
    {
        Schema::dropIfExists("user");
    });
}

}

UserModel:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'user';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

}

UserSeeder:

class UserSeeder extends DatabaseSeeder
{
public function run()
{
    $users = [
        [
            "username" => "ihkawiss",
            "password" => Hash::make("123456"),
            "email"    => "[email protected]"
        ]
    ];

    foreach ($users as $user)
    {
        User::create($user);
    }
}
}

DatabaseSeeder:

class DatabaseSeeder extends Seeder {

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    Eloquent::unguard();

    $this->call('UserSeeder');
}

}

Database Config:

return array(

/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/

'fetch' => PDO::FETCH_CLASS,

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default' => 'mysql',

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => array(

    'sqlite' => array(
        'driver'   => 'sqlite',
        'database' => __DIR__.'/../database/production.sqlite',
        'prefix'   => '',
    ),

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'laravel',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    'pgsql' => array(
        'driver'   => 'pgsql',
        'host'     => 'localhost',
        'database' => 'database',
        'username' => 'root',
        'password' => '',
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ),

    'sqlsrv' => array(
        'driver'   => 'sqlsrv',
        'host'     => 'localhost',
        'database' => 'database',
        'username' => 'root',
        'password' => '',
        'prefix'   => '',
    ),

),

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk have not actually be run in the databases.
|
*/

'migrations' => 'migrations',

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

'redis' => array(

    'cluster' => true,

    'default' => array(
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ),

),

);

Hope somebody can give me a hint here.

Best regards ihkawiss

3
I've now managed to get it working, but unfortunately only by a workaround. I created a table user, added a column which isn't created by the migration, run php artisan migrate which worked. After that I deleted the created column.ihkawiss
I stumbled on the same thing. The migration:create uses "Schema::table" which needs to be changed to "Schema:create". I think these templates can be modified - somewhere.jjwdesign

3 Answers

92
votes

In your CreateUserTable migration file, instead of Schema::table you have to use Schema::create.

The Schema::table is used to alter an existing table and the Schema::create is used to create new table.

Check the documentation:

So your user migration will be:

<?php

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

class CreateUserTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user', function(Blueprint $table) {
        {

            $table->increments("id",true);
            $table->string("username")->nullable()->default(null);
            $table->string("password")->nullable()->default(null);
            $table->string("email")->nullable()->default(null);
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists("user");
    }

}
2
votes

The underlying cause of this may be if the syntax used for creating the migration php artisan migrate ... is not quite correct. In this case the --create will not get picked up properly and you will see the Schema::table instead of the expected Schema::create. When a migration file is generated like this you might also be missing some of the other markers for a create migration such as the $table->increments('id'); and $table->timestamps();

For example, these two commands will not create a create table migration file as you might expect:

php artisan migrate:make create_tasks_table --table="tasks" --create
php artisan migrate:make create_tasks2_table --table=tasks2 --create

However, the command will not fail with an error. Instead laravel will create a migration file using Schema::table

I always just use the full syntax when creating a new migration file like this:

php artisan migrate:make create_tasks_table --table=tasks --create=tasks 

to avoid any issues like this.

1
votes

Sometimes it is caused by custom artisan commands. Some of these commands might initiate few classes. And because we did a rollback, the table cannot be found. Check you custom artisan commands. You can comment out the lines which are causing the trigger. Run the php artisan migrate command and then uncomment. This is what I had to do.