首頁  >  文章  >  後端開發  >  關於Laravel基礎Migrations的解析

關於Laravel基礎Migrations的解析

不言
不言原創
2018-07-10 13:49:222906瀏覽

一、Migration建立資料表與Seeder資料庫填入資料

資料庫遷移就像是資料庫的#版本控制#,可以讓你的團隊輕鬆修改並分享應用程式的資料庫架構

1.1 建立遷移

php artisan make:migration create_users_table --create=users

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

    新的遷移檔案會被放置在database/migrations 目錄中。每個遷移檔案的名稱都包含了一個時間戳,以便讓 Laravel 確認遷移的順序。
    --table--create 選項可用來指定資料表的名稱,或是該遷移執行時是否將建立的新資料表。

1.2 遷移結構

    遷移類別通常包含兩個方法:updown#。 up 方法可為資料庫新增新的資料表、欄位或索引,而 down 方法則是 up 方法的反向操作。可以在這兩個方法中使用 Laravel 資料庫結構產生器來建立以及修改資料表。

1.2.1 建立資料表

/**
     * 运行数据库迁移
     *
     * @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 為表格新增欄位

關於Laravel基礎Migrations的解析

資料表、欄位、索引:https://laravel-china.org/doc...

1.3 運行遷移

運行所有未完成的遷移:php artisan migrate

#1.4 回滾遷移

回滾最後一次遷移,可以使用rollback 指令:

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

1.5 使用Seeder方式向資料庫填入資料

1.5.1 寫Seeders

php artisan make:seeder UsersTableSeeder

1.5.2 資料庫填色

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

關於Laravel基礎Migrations的解析

    #利用模型工廠類別來大量建立測試資料

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

關於Laravel基礎Migrations的解析

關於Laravel基礎Migrations的解析

1.5.3 呼叫其他Seeders

    在DatabaseSeeder 類別中,你可以使用call 方法來運行其他的seed 類別。

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

1.5.4 執行Seeders

    預設情況下,db:seed 指令將執行DatabaseSeeder 類,這個類別可以用來呼叫其它Seed 類別。不過,你也可以使用--class 選項來指定一個特定的seeder 類別:

php artisan db:seed

php artisan db:seed --class=UsersTableSeeder

    你也可以使用migrate:refresh命令來填充資料庫,該命令會回滾並重新運行所有遷移。這個指令可以用來重建資料庫:

php artisan migrate:refresh --seed

二、模型

建立模型:

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

關於Laravel基礎Migrations的解析

關於Laravel基礎Migrations的解析

關於Laravel基礎Migrations的解析

關於Laravel基礎Migrations的解析

##三、路由

批次建立路由:(資源路由)

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

根據唯一字段值取得詳情,利於SEO關於Laravel基礎Migrations的解析    



Laravel 5.5 Nginx 設定:

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.1 快速驗證關於Laravel基礎Migrations的解析

4.2 表單請求驗證關於Laravel基礎Migrations的解析

php artisan make:request StoreBlogPost

差異與注意力

1.  find 和get

find: 透過主鍵傳回指定的資料

$result = Student::find(1001);
###get - 查詢多條資料結果###
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中文网!

相关推荐:

以上是關於Laravel基礎Migrations的解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn