Search code examples
phpmysqllaravellaravel-4artisan-migrate

Cant create table in laravel


i have this issue that i dont know exactly migration and seed db working in laravel (php artisan) and i have this model :

use Illuminate\Auth\UserInterface;


class User extends Eloquent implements UserInterface {
    public function getAuthIdentifier() {
        return $this->getKey();
    }
    public function getAuthPassword() {
        return $this->password;
    }
    public function cats(){
        return $this->hasMany('Cat');
    }
    public function owns(Cat $cat){
        return $this->id == $cat->owner;
    }
    public function canEdit(Cat $cat){
        return $this->is_admin or $this->owns($cat);
    }
    public function getRememberToken(){
        return $this->remember_token;
    }
    public function setRememberToken($value){
        $this->remember_token = $value;
    }
    public function getRememberTokenName(){
        return 'remember_token';
    }

}

this is my migration file:

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

class CreateUser extends Migration {

    public function up()
    {
        Schema::create('users', function($table){
            $table->increments('id');
            $table->string('username');
            $table->string('password');
            $table->boolean('is_admin');
            $table->timestamps();
        });

        Schema::table('cats', function($table){
            $table->integer('user_id')->nullable()->references('id')->on('users');
        });
    }


    public function down()
    {
        Schema::table('cats', function($table){
            $table->dropForeign('cats_user_id_foreign');
            $table->dropColumn('user_id');
        });
        Schema::drop('users');
    }

}

and at last my seeder code:

class UsersTableSeeder extends Seeder {
    public function run(){

        DB::table('users')->delete();

        User::create(array(
            'username' =>'admin', 'password' =>Hash::make('hunter2'), 'is_admin' =>true
        ));

        User::create(array(
           'username'=>'scott', 'password' =>Hash::make('tiger'), 'is_admin' => false
        ));
    }
}

when i try to migrate this i got this message :

$php artisan migrate --path="foo"

Nothing to migrate

and then when i try to :

$php artisan db:seed

this message shown to me:

[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.users' doesn't exist

please help me guys , Thank U :)

and config/database.php:

return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(

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

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

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

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

),


'migrations' => 'migrations',



'redis' => array(

    'cluster' => false,

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

),

);

Solution

  • It seems that in directory foo you don't have any migrations file or you already launched some migrations (do you use migrations from 2 directories?).

    Make sure you have inside foo your migration files and you haven't run any migrations earlier.

    EDIT

    And in case you have your migrations in default folder, you should run simply:

    php artisan migrate

    without using --path="foo"

    EDIT2

    You should probably change:

    $table->integer('user_id')->nullable()->references('id')->on('users');
    

    into:

    $table->dropColumn('user_id');
    $table->integer('user_id')->unsigned()->nullable();
    $table->foreign('user_id')->references('id')->on('users');