<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 數據表結構 ### 介紹 遷移和種子文件使您可以構建,修改和填充數據庫表。它們主要由[插件更新文件](https://octobercms.com/docs/plugin/updates)使用,并與插件的版本歷史記錄配對。所有類都存儲在`updates`插件目錄中。遷移過程應該講述一個有關數據庫歷史記錄的故事,并且可以向前和向后播放該故事以建立和刪除表。 ### [](https://octobercms.com/docs/database/structure#migration-structure)遷移結構 遷移文件應定義一個擴展`October\Rain\Database\Updates\Migration`該類的類,并包含兩個方法:`up`和`down`。該`up`方法用于向數據庫中添加新表,列或索引,而該`down`方法應簡單地反轉該`up`方法執行的操作。在這兩種方法中,您都可以使用[模式構建器](https://octobercms.com/docs/database/structure#creating-tables)來表達性地創建和修改表。例如,讓我們看一下創建`october_blog_posts`表的示例遷移: ~~~ <?php namespace Acme\Blog\Updates; use Schema; use October\Rain\Database\Updates\Migration; class CreatePostsTable extends Migration { public function up() { Schema::create('october_blog_posts', function($table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->string('title'); $table->string('slug')->index(); $table->text('excerpt')->nullable(); $table->text('content'); $table->timestamp('published_at')->nullable(); $table->boolean('is_published')->default(false); $table->timestamps(); }); } public function down() { Schema::drop('october_blog_posts'); } } ~~~ ### [](https://octobercms.com/docs/database/structure#creating-tables)建立表格 若要創建新的數據庫表,請使用外觀`create`上的方法`Schema`。該`create`方法接受兩個參數。第一個是表的名稱,第二個是的`Closure`,它接收用于定義新表的對象: ~~~ Schema::create('users', function ($table) { $table->increments('id'); }); ~~~ 當然,在創建表時,您可以使用模式構建器的任何[列方法](https://octobercms.com/docs/database/structure#creating-columns)來定義表的列。 #### 檢查表/列是否存在 您可以使用`hasTable`和`hasColumn`方法輕松檢查表或列是否存在: ~~~ if (Schema::hasTable('users')) { // } if (Schema::hasColumn('users', 'email')) { // } ~~~ #### 連接和存儲引擎 如果要在不是默認連接的數據庫連接上執行架構操作,請使用以下`connection`方法: ~~~ Schema::connection('foo')->create('users', function ($table) { $table->increments('id'); }); ~~~ 要為表設置存儲引擎,請`engine`在模式構建器上設置屬性: ~~~ Schema::create('users', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); }); ~~~ ### [](https://octobercms.com/docs/database/structure#renaming-and-dropping-tables)重命名/刪除表 要重命名現有數據庫表,請使用以下`rename`方法: ~~~ Schema::rename($from, $to); ~~~ 要刪除現有表,可以使用`drop`或`dropIfExists`方法: ~~~ Schema::drop('users'); Schema::dropIfExists('users'); ~~~ ### [](https://octobercms.com/docs/database/structure#creating-columns)創建列 要更新現有表,我們將`table`在`Schema`外觀上使用方法。與`create`方法類似,該`table`方法接受兩個參數,表的名稱和`Closure`,它接收一個可用于向表中添加列的對象: ~~~ Schema::table('users', function ($table) { $table->string('email'); }); ~~~ #### 可用的列類型 當然,架構構建器包含各種在創建表時可以使用的列類型: | 命令 | 描述 | | --- | --- | | `$table->bigIncrements('id');` | 使用等效的“ UNSIGNED BIG INTEGER”遞增ID(主鍵)。 | | `$table->bigInteger('votes');` | 相當于數據庫的BIGINT。 | | `$table->binary('data');` | 相當于數據庫的BLOB。 | | `$table->boolean('confirmed');` | 相當于數據庫的BOOLEAN。 | | `$table->char('name', 4);` | 等同于CHAR的長度。 | | `$table->date('created_at');` | 對應于數據庫的DATE。 | | `$table->dateTime('created_at');` | 相當于數據庫的DATETIME。 | | `$table->decimal('amount', 5, 2);` | 相當于DECIMAL的精度和規模。 | | `$table->double('column', 15, 8);` | 精確度為DOUBLE,精度為15位,小數點后為8位。 | | `$table->enum('choices', ['foo', 'bar']);` | 相當于數據庫的ENUM。 | | `$table->float('amount');` | 相當于數據庫的FLOAT。 | | `$table->increments('id');` | 使用等效的“ UNSIGNED INTEGER”遞增ID(主鍵)。 | | `$table->integer('votes');` | 相當于數據庫的INTEGER。 | | `$table->json('options');` | 與數據庫等效的JSON。 | | `$table->jsonb('options');` | 相當于數據庫的JSONB。 | | `$table->longText('description');` | LONGTEXT等效于數據庫。 | | `$table->mediumInteger('numbers');` | 相當于數據庫的MEDIUMINT。 | | `$table->mediumText('description');` | 相當于數據庫的MEDIUMTEXT。 | | `$table->morphs('taggable');` | 添加INTEGER`taggable_id`和STRING`taggable_type`。 | | `$table->nullableTimestamps();` | 與相同`timestamps()`,但允許NULL。 | | `$table->rememberToken();` | 添加`remember_token`為VARCHAR(100)NULL。 | | `$table->smallInteger('votes');` | 相當于數據庫的SMALLINT。 | | `$table->softDeletes();` | “添加”`deleted_at`列用于軟刪除。 | | `$table->string('email');` | VARCHAR等效列。 | | `$table->string('name', 100);` | VARCHAR等效于長度。 | | `$table->text('description');` | 等同于數據庫的TEXT。 | | `$table->time('sunrise');` | 相當于數據庫的TIME。 | | `$table->tinyInteger('numbers');` | 相當于數據庫的TINYINT。 | | `$table->timestamp('added_on');` | 相當于數據庫的TIMESTAMP。 | | `$table->timestamps();` | 添加`created_at`和`updated_at`列。 | #### 列修飾符 除了上面列出的列類型之外,添加列時還可以使用其他幾個列“修飾符”。例如,要使列“可為空”,可以使用以下`nullable`方法: ~~~ Schema::table('users', function ($table) { $table->string('email')->nullable(); }); ~~~ 以下是所有可用的列修飾符的列表。此列表不包括[索引修飾符](https://octobercms.com/docs/database/structure#creating-indexes): | 修飾符 | 描述 | | --- | --- | | `->nullable()` | 允許將NULL值插入到列中 | | `->default($value)` | 為列指定一個“默認”值 | | `->unsigned()` | 將`integer`列設置為`UNSIGNED` | | `->first()` | 將列“ first”放在表中(僅適用于MySQL) | | `->after('column')` | 將列放在另一列“之后”(僅適用于MySQL) | | `->comment('my comment')` | 在列中添加評論(僅適用于MySQL) | ### [](https://octobercms.com/docs/database/structure#modifying-columns)修改列 該`change`方法允許您將現有列修改為新類型,或修改列的屬性。例如,您可能希望增加字符串列的大小。要查看實際的`change`方法,讓我們將`name`列的大小從25增加到50: ~~~ Schema::table('users', function ($table) { $table->string('name', 50)->change(); }); ~~~ 我們還可以將列修改為可為空: ~~~ Schema::table('users', function ($table) { $table->string('name', 50)->nullable()->change(); }); ~~~ #### [](https://octobercms.com/docs/database/structure#renaming-columns)重命名列 要重命名列,可以使用`renameColumn`“模式”構建器上的方法: ~~~ Schema::table('users', function ($table) { $table->renameColumn('from', 'to'); }); ~~~ > **注意:**`enum`目前不支持使用列重命名表中的列。 ### [](https://octobercms.com/docs/database/structure#dropping-columns)刪除列 要刪除列,請使用`dropColumn`“模式”構建器上的方法: ~~~ Schema::table('users', function ($table) { $table->dropColumn('votes'); }); ~~~ 您可以通過將列名稱數組傳遞給`dropColumn`方法來從表中刪除多個列: ~~~ Schema::table('users', function ($table) { $table->dropColumn(['votes', 'avatar', 'location']); }); ~~~ ### [](https://octobercms.com/docs/database/structure#creating-indexes)創建索引 模式構建器支持幾種類型的索引。首先,讓我們看一個示例,該示例指定一列的值應唯一。要創建索引,我們可以簡單地將`unique`方法鏈接到列定義上: ~~~ $table->string('email')->unique(); ~~~ 或者,您可以在定義列之后創建索引。例如: ~~~ $table->unique('email'); ~~~ 您甚至可以將列數組傳遞給索引方法以創建復合索引: ~~~ $table->index(['account_id', 'created_at']); ~~~ 在大多數情況下,應手動為索引指定一個名稱作為第二個參數,以避免系統自動生成太長的名稱: ~~~ $table->index(['account_id', 'created_at'], 'account_created'); ~~~ #### 可用的索引類型 | 命令 | 描述 | | --- | --- | | `$table->primary('id');` | 添加一個主鍵。 | | `$table->primary(['first', 'last']);` | 添加組合鍵。 | | `$table->unique('email');` | 添加唯一索引。 | | `$table->index('state');` | 添加基本??索引。 | ### [](https://octobercms.com/docs/database/structure#dropping-indexes)刪除索引 要刪除索引,必須指定索引的名稱。如果沒有手動指定名稱,則系統將自動生成一個名稱,只需將表名稱,索引列的名稱和索引類型連接在一起。這里有些例子: | 命令 | 描述 | | --- | --- | | `$table->dropPrimary('users_id_primary');` | 從“用戶”表中刪除主鍵。 | | `$table->dropUnique('users_email_unique');` | 從“用戶”表中刪除唯一索引。 | | `$table->dropIndex('geo_state_index');` | 從“ geo”表中刪除基本索引。 | ### [](https://octobercms.com/docs/database/structure#foreign-key-constraints)外鍵約束 還支持創建外鍵約束,這些約束用于在數據庫級別強制引用完整性。例如,讓我們`user_id`在`posts`表上定義一個列,該`id`列引用表上的列`users`: ~~~ Schema::table('posts', function ($table) { $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users'); }); ~~~ 和以前一樣,您可以通過將第二個參數傳遞給`foreign`方法來手動指定約束的名稱: ~~~ $table->foreign('user_id', 'user_foreign') ->references('id') ->on('users'); ~~~ 您還可以為約束的“刪除時”和“更新時”屬性指定所需的操作: ~~~ $table->foreign('user_id') ->references('id') ->on('users') ->onDelete('cascade'); ~~~ 要刪除外鍵,可以使用`dropForeign`方法。外鍵約束使用與索引相同的命名約定。因此,如果未手動指定,我們將連接表名和約束中的列,然后在名稱后綴“ \_foreign”: ~~~ $table->dropForeign('posts_user_id_foreign'); ~~~ ### [](https://octobercms.com/docs/database/structure#seeder-structure)播種者結構 像遷移文件一樣,默認情況下,種子類僅包含一個方法:`run`并且應該擴展`Seeder`該類。`run`執行更新過程時將調用該方法。在這種方法中,您可以根據需要將數據插入數據庫中。您可以使用[查詢生成器](https://octobercms.com/docs/database/query)手動插入數據,也可以使用[模型類](https://octobercms.com/docs/database/model)。在下面的示例中,我們將使用方法`User`內的模型創建一個新用戶`run`: ~~~ <?php namespace Acme\Users\Updates; use Seeder; use Acme\Users\Models\User; class SeedUsersTable extends Seeder { public function run() { $user = User::create([ 'email' => 'user@example.com', 'login' => 'user', 'password' => 'password123', 'password_confirmation' => 'password123', 'first_name' => 'Actual', 'last_name' => 'Person', 'is_activated' => true ]); } } ~~~ 另外,也可以使用`Db::table`[查詢生成器](https://octobercms.com/docs/database/query)方法實現相同的目的: ~~~ public function run() { $user = Db::table('users')->insert([ 'email' => 'user@example.com', 'login' => 'user', [...] ]); } ~~~ ### [](https://octobercms.com/docs/database/structure#calling-additional-seeders)呼叫其他播種機 在`DatabaseSeeder`該類中,您可以使用該`call`方法執行其他種子類。使用該`call`方法可以將數據庫種子分解為多個文件,從而使單個種子類都不會變得太大。只需傳遞您希望運行的種子類的名稱即可: ~~~ /** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); $this->call('Acme\Users\Updates\UserTableSeeder'); $this->call('Acme\Users\Updates\PostsTableSeeder'); $this->call('Acme\Users\Updates\CommentsTableSeeder'); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看