我喜欢laravel因为能够创建迁移。
根据我的理解,laravel为我们提供了创建迁移的能力,我们可以重复相同的过程,而无需手动创建表和结构。我的问题:
同样的,
1)如果我希望我的数据(表的输入)也以某种方式存储,那么每当我更改数据库中的数据时,也可以恢复它或者也可以重新创建整个过程。
2)如果1不可能,那么我们是否可以有一种方法来保存数据库“初始”种子的方法。 (所以当我们“工厂”重置整个事情时,它也可以自动填充数据库的内容,而不仅仅是数据库的结构)
请问同样的参考文献?
我希望我能够清楚自己!
你认为Laravel令人难以置信是正确的!关于你的第一个问题。
1)如果我希望我的数据(表的输入)也以某种方式存储,那么每当我更改数据库中的数据时,也可以恢复它或者也可以重新创建整个过程。
如果要重新创建数据,则需要创建表播种器。要做到这一点,只需创建一个播种机和工匠与工匠。
php artisan make:seeder UsersTableSeeder
制作播种机后,您可以使用以下命令运行它:
composer dump-autoload && php artisan db:seed
如果您想在制作模型时同时创建控制器,播种机和工厂,请键入此工匠命令。
php artisan make:model User -fa
您可以在Laravel文档中查看有关创建播种机和工厂here的更多信息。
我会创建播种机,而不是弄乱你的迁移文件。这是一些例子。
图表1 - 文章工厂的示例(数据库/工厂/ ArticleFactory.php)
<?php
use Faker\Generator as Faker;
$factory->define(App\Article::class, function (Faker $faker) {
return [
'title' => $faker->text(50),
'slug' => $faker->unique()->slug,
'body' => $faker->text(200),
'user_id' => rand(1,10),
];
});
图表2 - 文章播种机示例(数据库/种子/ ArticleTableSeeder.php)
<?php
use Illuminate\Database\Seeder;
class ArticlesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Article::class, 10)->create();
}
}
图表3 - 文章迁移示例(database / migrations / 2018_05_13_create_articles_table.php)
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->string('slug');
$table->integer('media_id')->nullable();
$table->integer('user_id')->nullable(); // Owner of Article
$table->timestamps();
$table->softDeletes();
$table->index('slug');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
图表4 - DatabaseTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use Faker\Factory as Faker;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// Disable all mass assignment restrictions
Model::unguard();
// Seeds the Articles
$this->call(ArticlesTableSeeder::class);
然后,要完成工厂重置,您只需键入以下artisan命令即可:
php artisan migrate:db --fresh
php artisan db:种子
回答1)是的,每次运行迁移时,您都可以手动自动将数据插入表中。
答案2)问题一是可能的
例:
public function up()
{
// Create the table
Schema::create('users', function($table){
$table->increments('id');
$table->string('email', 255);
$table->string('password', 64);
$table->boolean('verified');
$table->string('token', 255);
$table->timestamps();
});
// Insert some stuff
DB::table('users')->insert(
array(
'email' => '[email protected]',
'verified' => true
)
);
}
虽然您可以使用迁移文件或播种器文件(如上面的答案中所述)进行数据迁移,但根据经验,我强烈建议您将此类迁移代码放在播种器文件中,而不是迁移文件中。
原因是运行单个文件迁移非常困难。迁移旨在一次性运行,或者自上次迁移完成后逐步运行迁移,迁移不是单独挑选的,请参阅迁移帮助:
php artisan migrate --help
Usage:
migrate [options]
Options:
--database[=DATABASE] The database connection to use.
--force Force the operation to run when in production.
--path[=PATH] The path of migrations files to be executed.
--pretend Dump the SQL queries that would be run.
--seed Indicates if the seed task should be re-run.
--step Force the migrations to be run so they can be rolled back individually.
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
Run the database migrations
您会注意到没有可用于运行精心挑选的迁移的选项,您可能希望有一天在数据迁移中执行此操作(例如:假设您只想将数据从一个旧表转移到另一个表,将其视为使用夜间cron作业或其他东西将数据从事务数据库移动到分析数据库。
但是这个选项可以在播种机中使用:
php artisan db:seed --help
Usage:
db:seed [options]
Options:
--class[=CLASS] The class name of the root seeder [default:
这使得它比迁移更灵活(更不用说播种数据全部与数据有关,这更适合您的任务)