gpt4 book ai didi

php - 无法在 laravel 中创建表

转载 作者:行者123 更新时间:2023-11-29 00:08:28 24 4
gpt4 key购买 nike

我有这个问题,我不知道在 laravel(php artisan)中工作的迁移和种子数据库,我有这个模型:

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';
}

}

这是我的迁移文件:

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');
}

}

最后是我的播种器代码:

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
));
}
}

当我尝试迁移它时,我收到了这条消息:

$php artisan migrate --path="foo"

Nothing to migrate

然后当我尝试:

$php artisan db:seed

显示给我的这条消息:

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

请大家帮帮我,谢谢你:)

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

),

);

最佳答案

似乎在目录 foo 中您没有任何迁移文件,或者您已经启动了一些迁移(您是否使用来自 2 个目录的迁移?)。

确保您在 foo 中有您的迁移文件并且之前没有运行任何迁移。

编辑

如果你的迁移在默认文件夹中,你应该简单地运行:

php artisan 迁移

不使用 --path="foo"

EDIT2

你可能应该改变:

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

进入:

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

关于php - 无法在 laravel 中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26399015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com