您好,谁能解释一下 laravel 迁移和模型的相关内容
Hi there, Can anyone explain about the laravel migration and model stuff
所以我确实阅读了 laravel 文档,但这对我帮助不大,我真正想做的是设置一个新数据库并创建一个 table 并插入一些数据。
但问题是我什至不知道这样做的顺序。例如是先构建模型还是进行迁移或 运行 迁移。
请帮我弄到这些东西。谢谢
在 laravel
1 中创建数据库。创建迁移
- 要创建迁移,请使用 make:migration
php artisan make:migration create_all_tables
- 创建迁移后
- 开启迁移"your_project\database\migrations"
- 创建架构
Schema::create('preferences', function(Blueprint $table){
$table->increments('id');
$table->string('key', 50);
$table->text('text');
$table->timestamps();
});
- Your migration look like this
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFlockTables extends Migration
{
public function up()
{
Schema::create('users', function(Blueprint $table){
$table->increments('id');
$table->string('user_name', 20);
$table->string('password', 60);
$table->string('full_name', 50);
$table->string('contact_number', 50);
$table->string('unit', 10);
$table->text('address');
$table->rememberToken();
$table->timestamps();
});
Schema::create('preferences', function(Blueprint $table){
$table->increments('id');
$table->string('key', 50);
$table->text('text');
$table->timestamps();
});
}
public function down()
{
Schema::drop('preferences');
Schema::drop('users');
}
}
- 数据库设置(forge 只是示例名称,您可以提供任何名称)
- 在“.env”文件中(本地设置)
DB_HOST=localhost
DB_DATABASE=forge
DB_USERNAME=root
DB_PASSWORD=
- In "your_project\config\database.php"
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
在 phpmyadmin 中创建名为 forge 的数据库
运行 迁移:php artisan 迁移
首先创建模型还是迁移完全取决于您。但是,如果您想使用您创建的模型 class,则 table 必须存在。
所以我确实阅读了 laravel 文档,但这对我帮助不大,我真正想做的是设置一个新数据库并创建一个 table 并插入一些数据。
但问题是我什至不知道这样做的顺序。例如是先构建模型还是进行迁移或 运行 迁移。
请帮我弄到这些东西。谢谢
在 laravel 1 中创建数据库。创建迁移 - 要创建迁移,请使用 make:migration
php artisan make:migration create_all_tables
- 创建迁移后
- 开启迁移"your_project\database\migrations"
- 创建架构
Schema::create('preferences', function(Blueprint $table){ $table->increments('id'); $table->string('key', 50); $table->text('text'); $table->timestamps(); });
- Your migration look like this
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFlockTables extends Migration
{
public function up()
{
Schema::create('users', function(Blueprint $table){
$table->increments('id');
$table->string('user_name', 20);
$table->string('password', 60);
$table->string('full_name', 50);
$table->string('contact_number', 50);
$table->string('unit', 10);
$table->text('address');
$table->rememberToken();
$table->timestamps();
});
Schema::create('preferences', function(Blueprint $table){
$table->increments('id');
$table->string('key', 50);
$table->text('text');
$table->timestamps();
});
}
public function down()
{
Schema::drop('preferences');
Schema::drop('users');
}
}
- 数据库设置(forge 只是示例名称,您可以提供任何名称)
- 在“.env”文件中(本地设置)
DB_HOST=localhost DB_DATABASE=forge DB_USERNAME=root DB_PASSWORD=
- In "your_project\config\database.php"
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
在 phpmyadmin 中创建名为 forge 的数据库
运行 迁移:php artisan 迁移
首先创建模型还是迁移完全取决于您。但是,如果您想使用您创建的模型 class,则 table 必须存在。