微信扫一扫打赏支持

laravel基础课程---16、数据迁移(数据库迁移是什么)

laravel基础课程---16、数据迁移(数据库迁移是什么)

一、总结

一句话总结:

是什么:数据库迁移就像是【数据库的版本控制】,可以让你的团队轻松修改并共享应用程序的数据库结构。
使用场景:解决让同事手动在数据库结构中添加字段的情况

数据库迁移就像是数据库的版本控制,可以让你的团队轻松修改并共享应用程序的数据库结构。迁移通常会搭配上 Laravel 的数据库结构构造器来让你方便地构建数据库结构。如果你曾经出现过让同事手动在数据库结构中添加字段的情况,数据库迁移可以解决你这个问题。

Laravel 的 Schema facade 对所有 Laravel 支持的数据库系统提供了创建和操作数据表的相应支持。

 

 

1、lavarel使用session老是报错怎么办?

查看session是否开启:session_start()

 

 

2、lavarel中打印数据方式?

dd打印:dd($arr)

 

3、lavarel如何移除session中的所有数据?

flush方法:$request->session()->flush();

forget 方法可以从 session 内删除一条数据。如果你想删除 session 内所有数据,则可以使用 flush 方法:

$request->session()->forget('key');

$request->session()->flush();

 

4、lavarel如何实现数据库的迁移(看示例代码使用:原理:肯定会生成迁移表记录迁移情况)?

创建数据库迁移:php artisan:php artisan make:migration create_table_news
使用迁移【例如新建表】:调用迁移中的 up方法(自己方法):php artisan migrate
回退迁移【例如删除表】:调用迁移中的 down方法(自己方法):php artisan migrate:rollback
数据库的迁移

0、数据库迁移就像是数据库的版本控制 (不建议初学者使用)

1、创建数据库迁移
    php artisan make:migration create_table_news

     public function up()
    {
        Schema::create('news', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('text');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('news');
    }

2、使用迁移
    php artisan migrate

    调用迁移中的 up方法

3、返回
    php artisan migrate:rollback

    返回迁移中的 down方法

 

 

5、lavarel中如何使用调试工具(上线了的话就关闭调试助手)?

效果:就是在网站下面出现一个调试工具:比如运行了什么sql,比如加载了什么页面都会提示,很方便
修改配置:在 D:\laravel\yzmedu\yzm2\config\app.php 添加一句话 182行:Barryvdh\Debugbar\ServiceProvider::class,
composer安装调试助手:composer require barryvdh/laravel-debugbar
1、使用调试工具(config\app.php配置文件中)
    在 D:\laravel\yzmedu\yzm2\config\app.php 添加一句话182行

    Barryvdh\Debugbar\ServiceProvider::class,

2、安装调试助手
    composer require barryvdh/laravel-debugbar

 

 

6、lavarel中如何创建自定义工具函数(不用include的方式)?

新建了函数的公共目录;比如 D:\laravel\yzmedu\yzm2\app\Common
修改 composer.json:在配置文件的autoload中:"files":["app/Common/function.php"]
重新加载配置文件:composer dump-auto
自定义函数

1、新建了函数的公共目录

    D:\laravel\yzmedu\yzm2\app\Common

2、修改 composer.json
    ,

  "files":[
      "app/Common/function.php"
  ]

3、重新加载配置文件
    composer dump-auto

 

 

 

 

二、laravel 如何使用composer自动加载自己定义的文件夹

参考:laravel 如何使用composer自动加载自己定义的文件夹 - 个人文章 - SegmentFault 思否
https://segmentfault.com/a/1190000010832055

一. 问题

当我们 clone下来一个laravel框架,接着就开始我们表演,但是我们根据业务需求需要创建一些自定义的文件夹,那么我们该如何加载他们呢,如何避免这类错误[Symfony\\Component\\Debug\\Exception\\FatalThrowableError] Class 'tools\\alyduanxin\\api\_demo\\SmsDemo' not found

 

二:分三步来解决这个问题

  • 在laravel 中项目根目录下创建自己的文件夹,例如我在项目根目录下创建了一个tools文件夹。
  • 在项目文件夹的根目录下找到composer.json文件,在autoload里添加psr-4节点

    "psr-4": {
                "tools\\": "tools/"
            }
  • 接着执行

    composer dump-autoload -o
  • 至此我们就加载成功啦

 

三. 解释一下 composer dump-autoload 这条命令

下面是composer官方文档中关于dump-autoload命令的解释:

dump-autoload

If you need to update the autoloader(类加载器) because of new classes in a classmap package for example, you can use "dump-autoload" to do that without having to go through an install or update.
Additionally, it can dump an optimized(优化) autoloader that converts(转化) PSR-0/4 packages into classmapones for performance(性能) reasons. In large applications with many classes, the autoloader can take up a substantial portion of every request's time. Using classmaps for everything is less convenient in development, but using this option you can still use PSR-0/4 for convenience and classmaps for performance.
Options:
--no-scripts: Skips the execution of all scripts defined in composer.json file.
--optimize (-o): Convert PSR-0/4 autoloading to classmap to get a faster autoloader. This is recommended especially for production, but can take a bit of time to run so it is currently not done by default.
--classmap-authoritative (-a): Autoload classes from the classmap only. Implicitly enables --optimize.
--apcu: Use APCu to cache found/not-found classes.
--no-dev: Disables autoload-dev rules.
其中官方推荐的参数是 -o,即 composer dump-autoload -o 
这个命令可以将PSR-0/4自动加载成classmap来获取一个更快速的类加载器,推荐生产环境使用,但是,可能需要一点时间来运行,因此目前不是默认

 

 

 
posted @ 2019-05-13 03:53  范仁义  阅读(388)  评论(0编辑  收藏  举报