search
HomeBackend DevelopmentPHP TutorialAnalysis of Laravel's basic Migrations
Analysis of Laravel's basic MigrationsJul 10, 2018 pm 01:49 PM
laravelmigrationModelrouting

1. Migration creates data tables and populates the Seeder database with data

Database migration is like the version control of the database, which allows Your team can easily modify and share the application's database structure

1.1 Create migrations

php artisan make:migration create_users_table --create=users

php artisan make:migration add_votes_to_users_table --table=users //添加字段

New migration files will be placed in database/migrations directory. The name of each migration file includes a timestamp to allow Laravel to confirm the order of migrations.
--table and --create options can be used to specify the name of the data table, or whether a new data table will be created when the migration is executed.

1.2 Migration structure

Migration classes usually contain two methods: up and down. The up method can add a new data table, field or index to the database, while the down method is the reverse operation of the up method. You can use the Laravel database structure generator in these two methods to create and modify data tables.

1.2.1 Create a data table

/**
     * 运行数据库迁移
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->comment('字段注解');
            $table->string('airline')->comment('字段注解');
            $table->timestamps();
        });
    }

    /**
     * 回滚数据库迁移
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }

1.2.2 Add fields to the table

Analysis of Laravels basic Migrations

##Data table, field, index :https://laravel-china.org/doc...

1.3 Run migration

Run all outstanding migrations:

php artisan migrate

1.4 Rollback migration

To roll back the last migration, you can use the

rollback command:

php artisan migrate:rollback
php artisan migrate:rollback --step=5 //回滚迁移的个数
php artisan migrate:reset //回滚应用程序中的所有迁移
php artisan migrate:refresh // 命令不仅会回滚数据库的所有迁移还会接着运行 migrate 命令
php artisan migrate  //恢复
1.5 Use the Seeder method to fill data into the database

1.5.1 Writing Seeders

php artisan make:seeder UsersTableSeeder

1.5.2 Database filling

 /**
     * 运行数据库填充
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'name' => str_random(10),
            'email' => str_random(10).'@gmail.com',
            'password' => bcrypt('secret'),
        ]);
    }

Analysis of Laravels basic Migrations

Use the model factory class to create test data in batches

php artisan make:factory PostFactory -m Post // -m 表示绑定的model

Analysis of Laravels basic Migrations

Analysis of Laravels basic Migrations

1.5.3 Call other Seeders

In the

DatabaseSeeder class, you You can use the call method to run other seed classes.

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $this->call([
        UsersTableSeeder::class,
        PostsTableSeeder::class,
        CommentsTableSeeder::class,
    ]);
}
1.5.4 Run Seeders

By default, the

db:seed command will run the DatabaseSeeder class, which can be used to call other Seed Class. However, you can also use the --class option to specify a specific seeder class:

php artisan db:seed

php artisan db:seed --class=UsersTableSeeder
You can also use

migrate:refresh command to populate the database, which rolls back and reruns all migrations. This command can be used to rebuild the database:

php artisan migrate:refresh --seed
2. Model

Create the model:

php artisan make:model Models/Goods
php artisan make:model Models/Goods -m  //同时生成对应的migration文件

Analysis of Laravels basic Migrations

Analysis of Laravels basic Migrations

Analysis of Laravels basic Migrations

Analysis of Laravels basic Migrations

3. Routing

Create routes in batches: (resource routing)

php artisan make:controller UserController --resource
Route::resource('user', 'UserController'); //批量一次性定义`7`个路由

According to unique fields Value to get details, which is beneficial to SEO
Analysis of Laravels basic Migrations

Laravel 5.5 Nginx configuration:

root /example.com/public;
location / {

    try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }

location = /robots.txt { access_log off; log_not_found off; }

4. Verification

4.1 Quick verification

Analysis of Laravels basic Migrations

4.2 Form request verification

php artisan make:request StoreBlogPost

Analysis of Laravels basic Migrations

Differences and Notes

1. find and get

find: Return the specified data through the primary key

$result = Student::find(1001);
get - Query multiple data results

DB::table("表名")->get();
DB::table("表名")->where(条件)->get();

2.模型与数据表的绑定

创建Model类型,方法里面声明两个受保护属性:$table(表名)和$primaryKey(主键)

<?php namespace App;   
use Illuminate\Database\Eloquent\Model;
class Student extends Model{
    protected $table = &#39;student&#39;;
    protected $primaryKey = &#39;id&#39;;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

The above is the detailed content of Analysis of Laravel's basic Migrations. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在 Windows 11 上检查 NAT 类型如何在 Windows 11 上检查 NAT 类型Apr 13, 2023 pm 10:22 PM

延迟死亡是在线游戏玩家可能发生的最糟糕的事情。但是您知道吗,它并不总是由网速慢引起的?与流行的看法相反,这通常是您的 NAT 类型的问题,并且不会通过简单地致电您的互联网服务提供商来解决。什么是 NAT,它有什么作用?网络地址转换或 NAT 是一种网络系统,它提供了一种将本地 IP 地址修改为更广泛的互联网地址的方法。这就是您能够在同一本地网络上的许多设备上使用单个 IP 地址的方式。NAT 作为路由器的一部分,基本上将您的路由器变成本地网络和更广泛的互联网之间的中间管理者。但是,不只有一个 N

Wi-Fi 没有有效的 IP 配置:如何修复Wi-Fi 没有有效的 IP 配置:如何修复Apr 13, 2023 pm 06:22 PM

重启你的电脑和路由器你知道该怎么做; 如果您致电 ISP 技术支持,他们会要求您重新启动网络硬件。这是有充分理由的,因为重新启动您的 PC 将清除可能与您的连接发生冲突的正在运行的应用程序和缓存。重新启动(反弹)您的路由器(通常是包含路由器和调制解调器的组合单元)将清除其缓存并重新建立可靠的在线连接。如果您还有一个单独的调制解调器,也请重新启动它。通过拔下电源按钮30 秒重新启动路由器,然后将其重新插入。启动路由器后,重新启动 PC 并查看您是否重新获得稳定的 Wi-Fi 连接。重新启用 Wi-

使用设置应用程序或路由器在 iPhone 上查找 Mac 地址的 5 大方法使用设置应用程序或路由器在 iPhone 上查找 Mac 地址的 5 大方法Apr 13, 2023 pm 05:46 PM

任何连接到互联网的设备都有两种类型的地址——物理地址和互联网地址。虽然 Internet 地址在全球范围内定位设备,但物理地址有助于识别连接到本地网络的特定设备。这个物理地址在技术上称为 MAC 地址,如果您想知道您的 iPhone 是否有一个,是的,所有手机(包括 iPhone)都有自己独有的 MAC 地址。什么是 MAC 地址?媒体访问控制或 MAC 地址是一种独特的指标,用于从连接到同一网络的其他设备中识别您的设备。如果您拥有可以连接到互联网的设备,它将注册一个 MAC 地址。此地址由占

解决“Windows 11 上的 DNS 服务器未响应”问题的 12 种方法解决“Windows 11 上的 DNS 服务器未响应”问题的 12 种方法Apr 15, 2023 pm 10:46 PM

什么是DNS?DNS是域名系统的首字母缩写词,它是一个分散的命名系统,所有计算机、服务器和更多试图连接到互联网的设备都使用它。DNS有助于识别您的PC和发送到它的流量,系统会自动破译并显示必要的信息。为什么我在Windows11上收到“DNS服务器没有响应”?这个问题可能有很多原因。有时,Windows可能会将网络问题误认为是DNS问题,而有时它很可能是第三方应用程序干扰了您的网络。最近对AVG防病毒软件的更新似乎是导致此问题的主要原因,禁用该更新似乎可以解决大多数用户的此问题

linux添加路由命令是什么linux添加路由命令是什么Jan 04, 2023 pm 01:49 PM

linux添加路由命令是“route”,linux添加路由的方法是:1、在“/etc/rc.local”里添加“route add -net 192.168.2.0/24 gw 192.168.3.254”;2、在“/etc/sysconfig/network”里添加“GATEWAY=gw-ip”到末尾;3、在“static-router”添加“any net ...”即可。

如何在 Windows 11 / 10 上解决无互联网安全问题如何在 Windows 11 / 10 上解决无互联网安全问题May 11, 2023 pm 10:07 PM

在Windows11/10计算机上看到的与互联网连接相关的问题之一是“无互联网,安全”错误消息。基本上,此错误消息表明系统已连接到网络,但由于连接存在问题,您无法打开任何网页并接收数据。在Windows中连接到任何网络时可能会遇到此错误,最好是在通过不在附近的WiFi路由器连接到Internet时。通常,当您检查系统托盘右下方的无线图标时,会看到一个黄色的小三角形,当您单击它时,会显示无Internet,安全消息。出现此错误消息没有具体原因,但配置设置的更改可能会导致您的路由器无法连接

用 NTP 时间服务器错误修复路由器失去联系的 3 种方法用 NTP 时间服务器错误修复路由器失去联系的 3 种方法May 22, 2023 pm 03:43 PM

连接和WiFi的问题可能会非常令人沮丧并显着降低生产力。计算机使用网络时间协议(NTP)进行时钟同步。在大多数情况下(如果不是全部),您的笔记本电脑使用NTP来跟踪时间。如果您的服务器因NTP时间服务器错误消息而失去联系,请阅读本文到底以了解如何修复它。当路由器的时间设置不正确时会发生什么?路由器的性能通常不受时间设置错误的影响,因此您的连接可能不会受到影响。但是,可能会出现一些问题。这些包括:使用路由器作为本地时间服务器的所有小工具的时间不正确。路由器日志数据中的时间戳将是错误的。如果由于

路由选择是osi模型中什么层的主要功能路由选择是osi模型中什么层的主要功能Dec 09, 2020 pm 05:48 PM

路由选择是osi模型中网络层的主要功能。osi模型是指开放式系统互联通信参考模型,是一种概念模型,由国际标准化组织提出,一个试图使各种计算机在世界范围内互连为网络的标准框架。OSI将计算机网络体系结构划分为七层:物理层、数据链路层、网络层、传输层、会话层、表示层和应用层。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools