Laravel Migration 基础
打赏作者

kanjiushi

楼主搞得不错,加油更新。

JellyBool

谢谢,只是录视频还没有达到预期那么好

JellyBool

@kanjiushi 常来看看

amoswu

期待更新,加油

sonics34

想问下网站表单输入框是什么字体,很漂亮啊

sonics34

最新更新是先发布在这里还是jellybool.com呢

JellyBool

谢谢,一直在更新呢 @amoswu

JellyBool

视频的话是会在这里,如果是博客的话,会两边同步 @sonics34

JellyBool

字体声明是这个

Lato,"Helvetica Neue","Segoe UI",Helvetica,Arial,sans-serif
sonics34

想问下如果不用命令行的话,如何执行up方法呢

JellyBool

你可以正常那样使用PHP的类实例化来操作,就是类似

$articleTable = new CreateArticlesTable();
$articleTable->up();

第二,这句话没看懂还有就是建立的schema表是不是用来检测up下建立的数据表是否存在的,你说的是migrations这个表么?

JellyBool

@sonics34 再描述一下你的问题?

sonics34

对的 就是migrations这个表 它の 作用

sonics34

好吧,又去翻了一下教程四,发现是自己理解错了……
博主请问文字版博客还往下更新不,期待11啊

JellyBool

会更,不过是会将这个视频做完吧可能。一直都想写的,就是录视频很花时间而已 @sonics34 慢慢来吧

inlofter

想知道Mac中的数据库管理的软件叫什么名字?@JellyBool

mailman
php artisan make:migration create_articles_table --create='articles'   

这个命令。中间create_articles_table 是怎么回事。如果我建其他表也要写这个吗??

JellyBool

只是文件的名字而已,你建其他的表就写其他的名字,比如一个comments表,你可以写:

php artisan make:migration create_comments_table --create=comments

还有就是,有代码的时候,使用markdown样式吧,这样好看很多,对双方都有好处 @mailman

野生奥特曼打小怪兽 回复 JellyBool

php artisan make:migration create_comments_table 这个命令仅仅是创建create_comments_table 这个文件,并没有创建表,–create=comments这个才是真正创建表。

mailman

markdown 还不是很会。
@JellyBool

JellyBool

稍微用心看十分钟就会了 @mailman
https://laravist.com/discussion/21

mailman

php artisan migration

JellyBool

使用英文输入法。。。 @mailman

mailman
<?php
Route::get('\',function(){} );
mailman

php artisan migration

JellyBool

大哥,练习的时候可不可以到这里 https://laravist.com/discussion/create

多行代码用三个^^^,代替,将^替换为esc下面那个符号 @mailman

dylan

@JellyBool 你的editor用的是什么字体?

dylan

@JellyBool 赞!多谢

zczhang

好喜欢这个评论框 这个是自己做的吗还是用的插件

JellyBool

算是自己写的吧 @zczhang

inlofter

@JellyBool 我之前使用make:migration create_posts_table,之后我直接在数据库删除了posts表,再使用make:migration create_articles_table,创建了一张articles表。

现在需要根据现在的migration文件来重置数据库,使用php artisan migrate:refresh/reset等命令时,出现下列错误:

➜  laravel  php artisan migrate:refresh
PHP Fatal error:  Class 'CreatePostsTable' not found in /home/wwwroot/laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php on line 324
                           
  [Symfony\Component\Debug\Exception\FatalErrorException]  
  Class 'CreatePostsTable' not found                       

即它找不到我之前创建,后来又直接在数据库删除的post表的migration文件。问题是,这个create_posts_table.php文件我早就删除了,不知道如何refresh/reset操作。

使用Find in Path命令在整个project中也找不到任何包含CreatePostsTable的文件,实在想不出怎么解决这个问题了。

inlofter

@JellyBool 晕,问题解决了,看来我考虑的还是不全面啊。没考虑到数据库中的migration表,直接删除了事。这么简单竟然想不到。

toreal

@JellyBool jelly 微博是你本人吗?

JellyBool

哪个微博?底部这个链接是的。。。

toreal

@JellyBool 嗯 就是这个微博!!jelly 你还在上学?

JellyBool

恩,微博是的,还在上学 @toreal

toreal

@JellyBool 哇哦~~很棒!!真的很棒!!我说呢!!我一上班 就懒的不行!也没时间学学弄弄些东西!!

JellyBool

谢谢,常来看看呗 @toreal

WaterChan

按照楼主视频的教学,对article表实现php artisan migrate时报错了 为什么字段会有重复?

[Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created
_at' (SQL: create table `articles` (`id` int unsigned not null auto_increme
nt primary key, `title` varchar(255) not null, `content` text not null, `cr
eated_at` timestamp default 0 not null, `updated_at` timestamp default 0 no
t null, `created_at` timestamp default 0 not null, `updated_at` timestamp d
efault 0 not null) default character set utf8 collate utf8_unicode_ci)

[PDOException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created
_at'

PS:已经在SegmentFault上解决了 麻烦楼主了

CevinJohnny

使用 php artisan migrate 命令的时候,为什么会出现这个问题呢…

CevinJohnny

@JellyBool 已解决,thx~

yueping1

@JellyBool 你好,我想问下,我用php artisan migrate这个命令的时候看不到那两个表出现,会出现nothing to migrate

ksir

php artisan make:mode Article 也是可以的

wenwen1993

赞一个

ksir

这是什么问题

JellyBool

--table=articles ? 你是写的=么? @ksir

clara

我实在虚拟机下使用的,请问这种情况是怎么回事?

502爱讲520

這個終端是mac原有的嗎?

JellyBool

iterm2 @502爱讲520

_xurong
D:\wamp\www\blog>php artisan make:migration create_articles_table --create-articles

  [RuntimeException]
  The "--create-articles" option does not exist.

请问这是什么回事呢

JellyBool
php artisan make:migration create_articles_table  --create=articles

@_xurong

_xurong

原来是"=",有点模糊没看清233333

_xurong

删除和修改一个字段怎么办?

thinkings

@JellyBool 你好,请问您说的那个MYSQL的管理软件名称是什么 。
我这有个麻烦问题,把PUBLIC设成WEB根目录了,然后ln phpmyadmin.php 到 public的目录下时,访问好像会触发APACHE的htaccess跳转到/

JellyBool

哎,直接使用artisan serve吧,软件是sequel pro

thinkings

嗯。这软件 只能MAC能用?我这UBUNTU整了个EMMA在用。

littlexia

很棒的视频,顺便问一下,楼主使用的是什么编辑器呀。。新手路过。

JellyBool

phpstorm @littlexia

littlexia

谢谢!

yijiang

看视频看到想睡觉~这个还是要动手才知道问题。 社长威武。

JellyBool

额。。你只是看而已,还是认真动手跟着视频敲代码吧,这样肯定不会想睡觉啊 @yijiang

李坤W

Access denied for user ‘homestead’@‘localhost’ (using password: YES)
在创建楼主说的表时出现这样,这是什么原因?

李坤W

已经解决了,原来配置没弄

dotasfans

请问博主,你讲的前面新建的表的表名是articles,在后面新建model时,建立的model名是Article。然后你说,框架会自动识别出来这个model对应的就是之前的articles表。在这里,我有一个疑问,首先,这两个名字是有大小写不同的,就算不区分大小写,他们一个有s一个没s,明显名字不一样,那么框架是怎么识别到的呢!总不至于我随便怎么写都能自动识别 啊!

JellyBool

基本上按照英文的单复数了识别的 @dotasfans

Neytoph

楼主,我按照你的新加了一个intro字段后,rollback的时候

Class ‘AddIntroColumnToAtricles’ not found
这是什么情况啊

JellyBool

额,是你没有按照步骤来,或者你执行一下composer autoload @Neytoph

julytian

@JellyBool ,按照你教程新加一个intro字段时,执行php artisan migrate时出现这些错误,怎么回事呢?

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.articles' doesn't exist(SQL: alter table `article  s` add `intro` varchar(255) not null) 
JellyBool

应该是你执行顺序的问题 @julytian

julytian

@JellyBool , 解决了,谢谢

LiucaiHp
E:\xampp\htdocs\laravel>php artisan migrate


  [PDOException]
  SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (usin
  g password: YES)

配置文件都配好了怎么还出现这个

wangdong

非常完美的视频,也非常感谢Jelly大哥,作为学生党我要剩下点钱来学习laravel的教学视频

JellyBool

谢谢,常来看看就行 @wangdong

wangdong

@JellyBool
刚学习呢

callmesoul

楼主问下,添加字段,UP创建后为什么还要Down drop掉呢?很不理解

JellyBool

roll back用的 @callmesoul

少年钰的烦恼

Jelly,请问没有homestead的话DB_HOST 、DB_DATABASE 、DB_USERNAME 、DB_PASSWORD该怎么填?(网络不好下载homestead总是超时。。)小白问题,求轻拍> <

callmesoul

楼主我把项目从家里啦回公司或者git同步到公司都会报这个错,不知道解决啊。

偶叫懒猪

老师我想问下,这个数据库文件管理对存在的表有保护吗? 之前的开发也是用类似这种方式来处理的,遇到过两个问题。一个是字段名被修改或是添加字段,视频上基本能解决。
另一个就是表存在,之前已经有做时间来保护,但是开发人多的话,会存在自己不知道哪些表或是哪些字段已经添加过了,当然这是在开发阶段。

JellyBool

就写你的本地的数据库名,用户名,密码呗 @少年钰的烦恼

Frank-Zhu

楼主,为啥我的直接执行 php artisan make:migration create_comments_table 命令没有自动生成migration文件,controller和model这些可以自动生成,唯独migration无法生成

Ghostdar

想问下,视频中输入命令行那个lavarel5 是代表在那个目录下面吗?

JellyBool 回复 Ghostdar

对,是在 Laravel 项目的目录下

zhaoritian

不明白如果不用命令行,怎么创建表,请朋友们帮我解决一下,谢谢

JellyBool 回复 zhaoritian

自己写一个 migration 的类,或者你直接用 sql 创建表,都是可以的。laravel 的migration 是为了方便数据库的管理

zhaoritian 回复 JellyBool

谢谢楼主,麻烦你了,懂了

liudong0763

老师,请问如何快速移到行尾并且打上分号?

JellyBool 回复 liudong0763

command + -> 再写分号

liudong0763 回复 JellyBool

sublime 可以录制宏,直接CTRL+;就行了,PHPSTORM有录制宏这样的功能么

liudong0763

doctrine/dbal 貌似本身就有,不用再安装耶,老师是么

jaelin

楼主的iterm2的主题是什么啊

JellyBool 回复 jaelin

cloud…没记错是这个

harverychina 回复 JellyBool

好像没有这个主题cloud,请问您用是什么主题?

wx630370

加油,不错,谢谢楼主!

wangtaihong

###Many Thanks!

harverychina

我想问一下老师,你用命令行工具是iTerm吗?你用的是什么配色方案?

harverychina

请问课程中最后安装的数据表字段管理工具这样安装是不是成功啦?@JellyBool
snip20161123_1

JellyBool 回复 harverychina

恩,是成功了的

harverychina 回复 JellyBool

谢谢!@JellyBool

mikezhang

楼主,有没有artisan命令可以同时创建表和Model?

JellyBool 回复 mikezhang

php artisan make:model Article -m

mikezhang 回复 JellyBool

谢谢,前段时间面试问到了

flyweights

看完了这节,我突然有个想法,如果一个长期维护的项目反复添加和删除字段,那么migration是不是越来越来多,变得非常复杂,会不会这样呢?

liaopujian

请问一下楼主,我使用的是windows xampp环境,Laravel5.4版本,我先是执行了一次migrate命令生成了user表,然后创建了Articles的文件,接着使用migrate命令时,显示user表已经存在,并且articles表也无法创建成功,请问这是什么原因?

言心言志

很赞~让菜鸟找到了依赖

JellyBool 回复 言心言志

哈哈哈。感谢支持啊!!

jacks12

问题解决了

znn2017

哈哈 看到了无挑 在石饭留言

JellyBool 回复 znn2017

可以的…刘大神很厉害

jjoker

感谢楼主分享!

LWJ

我想问下 最后的减:我已经composer require doctine/dbal 成功了, 是不是执行php artisan make:migration sub_intro_column_to_articles --table=articles

Administrator@PC-20151009TSXF MINGW64 /e/phpStudy/WWW/laravel-v5.1.11
$ php artisan make:migration sub_intro_column_toarticles --table=articles
Created Migration: 2017_04_07_155950_sub_intro_column_toarticles

Administrator@PC-20151009TSXF MINGW64 /e/phpStudy/WWW/laravel-v5.1.11
$ php artisan migrate
Migrated: 2017_04_07_155950_sub_intro_column_toarticles

Administrator@PC-20151009TSXF MINGW64 /e/phpStudy/WWW/laravel-v5.1.11

还是有intro

LWJ

一天到晚都在磕楼主的视频~~

LWJ

前面的问题搞懂了,我想请教下楼主 每次增加或减少都需要 php artisan make:migration sub_intro_column_toarticles --table=articles这样的命令,然后再$ php artisan migrate
么? 不能 就改了up 或者down的内容就可以php artisan migrate 了?是不是我少了一条更新命令,希望有人帮忙解答下

JellyBool 回复 LWJ

不能 就改了up 或者down的内容就可以php artisan migrate 了

可以,但是不推荐这样做而已

LWJ 回复 JellyBool

可是我测试的结果是up或者down的内容只能php artisan migrate一次。除非重新生成 php artisan make:migration sub_intro_column_toarticles --table=articles,是不是还有其它的命令?楼主好喔!

JellyBool 回复 LWJ

1.php artisan make:migration sub_intro_column_toarticles --table=articles

这是什么?你想要做什么?

  1. up 对应的是 artisan migrate,down 对应的是 artisan migrate:rollback
LWJ 回复 JellyBool

懂了,谢谢你的回答。

野生奥特曼打小怪兽

楼主厉害!感觉就你讲得实在!别的都是花架子!对了,mac里面管理mysql的软件叫什么?你说的那个软件名字具体是哪一个呀?

野生奥特曼打小怪兽 回复 JellyBool

后面看评论看到了,谢谢你哈!讲的真不错!希望后面还有更多精彩视频!我愿意付费

zjmax

楼主呀 这是什么错误呀

zjmax

用这命令出现以下错误 @JellyBool
D:\phpStudy\WWW\laravel>php artisan migrate:rollback

[Symfony\Component\Debug\Exception\FatalThrowableError]
Fatal error: Class ‘CreasteArticlesTable’ not found

JellyBool 回复 zjmax

要不就是写错单词了,要不就删错文件了

asea

不错

jjoker

个人认为不如直接使用 mysql 命令创建表来的爽快。

a359611223 回复 jjoker

我觉得这样方便迁移

TimeIsGoOn

···
php artisan migrate:rollback

#创建表articles,生成migration的脚本
php artisan make:migration create_articles_table --create=articles

#修改表article的字段:新增字段intro,生成migration的脚本
php artisan make:migration add_intro_column_to_aritcles --table=articles

资料备注

PHP Migrations——Doctrine Migrations教程
https://my.oschina.net/lyaohe/blog/741477

还在随手修改数据表?!试试DB Migrations吧
http://blog.chxj.name/doctrine-migrations/

migrations:地址
https://packagist.org/packages/doctrine/migrations
···

soyoung 回复 TimeIsGoOn

看到你在这里很活跃,可以加你QQ平时交流一下技术吗?

wang123good

to jelly:
··· php
[InvalidArgumentException]
Database [laravel] not configured.
···
这个错误该如何解决?

已经解决了,laravel5.4更新了database的api,修改一下就解决了

wang123good

‘ [InvalidArgumentException]
Database [laravel] not configured.’

wang123good

‘ [InvalidArgumentException]
Database [laravel] not configured.’

wang123good
   public function(){
} 
sdfcurry30

数据迁移中用的类似 navicat 的数据库是什么软件?

JellyBool 回复 sdfcurry30

视频用的是 sequel pro

sdfcurry30 回复 JellyBool

请教您一个问题,门面是什么?

不了了了了了之

按视频migrate完了以后就有了这个
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [1045] Access denied for user ‘homestead’@‘localhost’ (using password: YES) (SQL: select * from information_schema.tables where table_schema = homestead and
table_name = migrations)
是为什么?我没有创建数据库还是没开启?
要在哪改配置?

不了了了了了之

另外按照composer安装完以后的env里的配置sequel pro,也提示一样的问题,连接错误。
这是为什么?
目前我猜测是可能没装homestead,这个东西必要吗?如果没有的话,可以用localhost运行吗?要怎么配置呀?

JellyBool 回复 不了了了了了之

可以使用 localhost 的啊,你改一下 env 文件里面的 DATABASE_USERNAME 类似的配置,对应你的本机的数据库的用户名和密码就 OK,完全可以直接使用 php artisan serve 来进行开发的。

lion201314

为什么我执行laravel程序那么慢呢

JellyBool 回复 lion201314

不知道。。。

zjmax

D:\phpStudy\WWW\laravel>php artisan make:migration add_intro_column_to_articles --table=articles

[ErrorException]
include(D:\phpStudy\WWW\laravel\vendor\composer/…/…/database/migrations/2017_11_21_062149_add_intro_column_to_articles.php): failed to op
en stream: No such file or directory
这个问题怎么解决呀@Jellybool

JellyBool 回复 zjmax

migration 目录有这个文件:2017_11_21_062149_add_intro_column_to_articles?

你设置过 migration 的其他配置么

zjmax 回复 JellyBool

我之前把这2017_11_21_062149_add_intro_column_to_articles.php文件删除了 现在再添加字段 用着命令错误

JellyBool 回复 zjmax

那很正常啊,你删了文件怎么执行 migrate。。。

你到数据库中的 migrations 这个表中,找到 2017_11_21_062149_add_intro_column_to_articles 这个相关的记录,删除这个记录就好

zjmax 回复 JellyBool

谢谢 解决了 现在 哪个 illuminate/html 不能用了吗

Ericxie

你好楼主,我在phpstorm terminal运行 php artisan 会弹出以下错误
Parse error: syntax error, unexpected ‘?’, expecting variable (T_VARIABLE) in /Users/zhijiexie/Sites/task/vendor/symfony/console/Output/Output.php on line 40

但是我在iterms 下面运行就正常。请问是什么原因呢?@ Jellybool

JellyBool 回复 Ericxie

phpstorm 使用的 php 版本太低了吧

Ericxie 回复 JellyBool

的确是版本的原因,使用新版本之后问题就解决了?

harace202

请问下,多次在同一个数据表中添加或修改字段,是否每次都得用php artisan make:migration sub_intro_column_toarticles --table=articles这样的命令生成文件在去实行php artisan migrate命令?

JellyBool 回复 harace202

嗯,我目前是这样的。所以在开始项目的时候就应该想好表的结构

harace202

好的,知道了 谢谢

fox_999

非常感谢楼主的分享,有个小问题请教,在创建插入新列intro的时候,既然上面创建了,为什么下面还要drop掉?

wujianhua 回复 fox_999

为了回滚的时候用 我觉得是这样的

wujianhua

您好,我想单独回滚某一个文件可以做到吗?还是只有回滚到第几次这样啊

ddwzdd

public function boot()
{
Schema::defaultStringLength(191);

}