1

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,
    ),

),

);
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57

2 Answers2

1

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');
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • @Nabialek : first time i treid php artsian migrate with out any path and this commend run and try to migrate my last db , after that i try this with path – ᴀʀᴍᴀɴ Oct 16 '14 at 08:06
  • @ArmanKuroKy And when do you have your migration file? Do you have it in `foo` directory? What's the filename? And do you have already inside your database `migrations` table created? – Marcin Nabiałek Oct 16 '14 at 08:09
  • @Nabialek let me say it from first , i use this command ' php artisan migrate:make create_users ' , this commend bring a 2014_10_15_201401_create_user.php for me in migration folder and after that i write my code init and use ' php artisan migrate --path=this file path ' and nothing to migrate appears – ᴀʀᴍᴀɴ Oct 16 '14 at 08:12
  • @ArmanKuroKy If file was created in default migration folder (it's `app/database/migrations` you should just run `php artisan migrate` without using `--path="foo"`. So try running just `php artisan migrate` – Marcin Nabiałek Oct 16 '14 at 08:15
  • @Nabialek i try this but this command try to migrate my last migration file that is 2014_10_14_114343_add_cats_and_breeds_table.php in that folder – ᴀʀᴍᴀɴ Oct 16 '14 at 08:18
  • @ArmanKuroKy So it seems you have already migrated this file so you cannot migrate it again. As I said you should look in your database do you have `migrations` table and what's exactly in it. – Marcin Nabiałek Oct 16 '14 at 08:23
  • @ArmanKuroKy Do you get any message when you run `php artisan migrate` ? – Marcin Nabiałek Oct 16 '14 at 08:31
  • @Nabialek yes this : Base table or view allready exists : 1050 Table 'cats' already exists – ᴀʀᴍᴀɴ Oct 16 '14 at 08:34
  • nothing changed , but thank u , i think error must be more deeper than code – ᴀʀᴍᴀɴ Oct 16 '14 at 08:49
  • @ArmanKuroKy You can try removing from `up` function `cats` related part and check if migration will work. I think this is not migration issue but your database structure – Marcin Nabiałek Oct 16 '14 at 08:51
  • i think i found error , i delete all tables from laravel database except migration , and i try to `php artisan migrate` and i got this error `Call to undefined method Illuminate\\Database\\Schema\\Blueprint::increments()` this command create database but dont work and after this migration table is empty yet – ᴀʀᴍᴀɴ Oct 16 '14 at 09:05
  • @Nabialek http://stackoverflow.com/questions/26400802/call-to-undefined-method-illuminate-database-schema-blueprintincrements – ᴀʀᴍᴀɴ Oct 16 '14 at 09:27
0

It may be that when you tried to migrate the first time, it logged the migration into your migration table. Check your migrations table and let me know what you see.

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
Jeff
  • 2,018
  • 2
  • 15
  • 18
  • Yes, if you see one saying something that suggests your users table was migrated, then it means artisan thinks it's already migrated, what you can do to fix it is to delete the migrations table and rerun all migrations – Jeff Oct 16 '14 at 08:23
  • Where are your migration files. EDIT: try running composer dump-autoload then try running php artisan migrate again – Jeff Oct 16 '14 at 08:25
  • php artsian migrate try to migrate my last file not this file , and i dont know why , after this i tried to pass path to migrate – ᴀʀᴍᴀɴ Oct 16 '14 at 08:31
  • It should be working, the only problem i can think of is that it failed the first migration, but logged it anyways. check every database in your mysql server and look for a table called migrations. – Jeff Oct 16 '14 at 08:44