博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
laravel怎么使用工匠_工匠和Laravel移民
阅读量:2507 次
发布时间:2019-05-11

本文共 5275 字,大约阅读时间需要 17 分钟。

laravel怎么使用工匠

For those who struggle with maintaining their database schema, or who have problems applying updates and often revert them, there is a solution. Laravel, the MVC framework which I wrote about previously, offers you migrations.

对于那些在维护数据库架构上遇到困难的人,或者在应用更新时遇到问题并且经常还原它们的人,有一个解决方案。 Laravel是我之前撰写的MVC框架,可为您提供迁移。

In short, migrations are files which contain a class definition with both an up() and a down() method. The up() method is run when the migration is executed to apply changes to the database. The down() method is run to revert the changes. If you need to update the database, you just create a new migration and voilà. Not happy with it? Revert it and you’re back on track.

简而言之,迁移是包含同时具有up()down()方法的类定义的文件。 当执行迁移以将更改应用于数据库时,将运行up()方法。 运行down()方法以还原更改。 如果需要更新数据库,则只需创建一个新的迁移和模板即可。 不满意吗? 还原它,您就回到了正轨。

It all seems pretty cool, doesn’t it? But how does it work for Laravel? First you have to configure your database connection, and then you use Artisan, Laravel’s command line interface, to install the migrations table and run, revert, create… migrations.

一切看起来都很酷,不是吗? 但是它对Laravel如何起作用? 首先,您必须配置数据库连接,然后使用Laravel的命令行界面Artisan安装迁移表并运行,还原,创建…迁移。

Open a console and navigate to the root directory of your Laravel install. Run the following command:

打开控制台,然后导航到Laravel安装的根目录。 运行以下命令:

php artisan migrate:install

This command causes Artisan to create a special table in your database to keep track of what migrations have already been executed.

此命令使Artisan在您的数据库中创建一个特殊表,以跟踪已经执行了哪些迁移。

To create a new migration, run this command:

要创建新的迁移,请运行以下命令:

php artisan migrate:make create_users_table

This creates the migration file which handles the users table. You can find your migration file inside the application/migrations folder. Artisan adds the date and time of the command’s execution as a prefix to the file, so the file would be named something like “2012_07_25_071925_create_users_table.php”. Be sure to use a descriptive name so that it’s clear what the migration does from just a glance.

这将创建用于处理用户表的迁移文件。 您可以在application/migrations文件夹中找到您的迁移文件。 Artisan将命令执行的日期和时间作为文件的前缀添加,因此该文件将被命名为“ 2012_07_25_071925_create_users_table.php”。 确保使用描述性名称,这样一眼就能清楚看出迁移的目的。

Open the file and you’ll see a class with the two methods up() and down().

打开文件,您将看到带有两个方法up()down()

increments("id");$table->string("username", 32);$table->string("email", 320);$table->string("password", 64);$table->timestamps();});}public function down() {Schema::drop("users");}}

The up() method runs when the migration is executed and creates the users table which holds five columns. The first is an auto-incrementing ID column, followed by VARCHAR columns for a username, email, and password. The first parameter to string() is the name of the column (e.g. “username”) and the second is the size of the column (e.g. 32 characters long). The final columns are created by the timestamps() method which creates “created_at” and “updated_at” columns.

up()方法在执行迁移时运行,并创建包含五列的users表。 第一个是自动递增的ID列,其后是用于用户名,电子邮件和密码的VARCHAR列。 string()的第一个参数是列的名称(例如“用户名”),第二个参数是列的大小(例如32个字符长)。 最后的列由timestamps()方法创建,该方法创建“ created_at”和“ updated_at”列。

The down() method is simpler than its predecessor and simply tells the database to drop the users table.

down()方法比其前任方法简单,只是告诉数据库删除用户表。

Now this file won’t do anything if it just sits there. To execute all outstanding migrations, run:

现在,只要放在该文件中,它就什么也不做。 要执行所有未完成的迁移,请运行:

php artisan migrate

Currently it is not possible to run a specific migration. You can however run all the migrations in the application folder by the command:

当前无法运行特定的迁移。 但是,您可以通过以下命令在应用程序文件夹中运行所有迁移:

php artisan migrate application

You can do the same for a specific bundle by using the same command but with the bundle’s name.

您可以通过使用相同的命令,但使用束的名称来对特定束执行相同的操作。

Now let’s say you realize that you made a design error and you want to roll back the last migration run. Simply type:

现在,假设您意识到自己犯了一个设计错误,并且想要回滚上一次迁移运行。 只需键入:

php artisan migrate:rollback

It’s not yet possible to automatically roll-back to a point before a specific migration, so you’ll have to run the command repeatedly until you reach that migration. You can however reset all migrations that you’ve ever ran just by running:

尚无法自动回滚到特定迁移之前的某个点,因此您必须重复运行该命令,直到完成该迁移为止。 但是,您可以仅通过运行以下命令来重置曾经运行过的所有迁移:

php artisan migrate:reset

As it stands now, the database would allow users to register the same username or email address any number of times. We want to restrict that and only allow an address or username to be used once. Of course, we don’t want to roll back the previous migration because we would lose data if we did, so instead we create a new one.

目前,该数据库将允许用户多次注册相同的用户名或电子邮件地址。 我们希望限制此限制,并且只允许使用一个地址或用户名一次。 当然,我们不想回滚上一个迁移,因为如果这样做,我们将丢失数据,因此我们创建了一个新迁移。

php artisan migrate:make users_add_username_email

The up() method should add a unique index on the fields, like this:

up()方法应在字段上添加唯一索引,如下所示:

unique("username");$table->unique("email");});}In the down() method, you want simply to remove the indexes.
drop_unique("username");$table->drop_unique("email");}

That’s it! I hope you find Laravel’s migrations a solution to avoid all the hours of pain you’ve gone through managing your database schema. See you next time!

而已! 我希望您能找到Laravel的迁移解决方案,从而避免管理数据库模式所经历的所有麻烦。 下次见!

Image via

图片来自

翻译自:

laravel怎么使用工匠

转载地址:http://qmrgb.baihongyu.com/

你可能感兴趣的文章
树结构练习——排序二叉树的中序遍历
查看>>
AC自动机模板
查看>>
python 基本语法
查看>>
Swift - 点击箭头旋转
查看>>
git配置
查看>>
【hexo】01安装
查看>>
CI框架源码学习笔记2——Common.php
查看>>
005---书籍添加和编辑的提交数据
查看>>
使用case语句给字体改变颜色
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
JSP九大内置对象及四个作用域
查看>>
ConnectionString 属性尚未初始化
查看>>
数据结构-栈 C和C++的实现
查看>>
MySQL基本命令和常用数据库对象
查看>>
poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
查看>>
进程和线程概念及原理
查看>>
Lucene、ES好文章
查看>>
android 生命周期
查看>>
jquery--this
查看>>