Laravel 速查表
显示全部 1. Artisan 2. Auth 3. Blade 4. Cache 5. Collection 6. Composer 7. Config 8. Container 9. Cookie 10. DB 11. Environment 12. Event 13. File 14. Form 15. HTML 16. Helper 17. Input 18. Lang 19. Log 20. Mail 21. Model 22. Pagination 23. Queue 24. Redirect 25. Request 26. Response 27. Route 28. SSH 29. Schema 30. Security 31. Session 32. String 33. URL 34. UnitTest 35. Validation 36. View
Menu

Schema

未匹配的标注
本文档最新版为 9.x,旧版本可能放弃维护,推荐阅读最新版!
// 创建指定数据表
 Schema::create('table', function($table)
{
  $table->increments('id');
});
// 指定一个连接
 Schema::connection('foo')->create('table', function($table){});
// 通过给定的名称来重命名数据表
 Schema::rename($from, $to);
// 移除指定数据表
 Schema::drop('table');
// 当数据表存在时, 将指定数据表移除
 Schema::dropIfExists('table');
// 判断数据表是否存在
 Schema::hasTable('table');
// 判断数据表是否有该列
 Schema::hasColumn('table', 'column');
// 更新一个已存在的数据表
 Schema::table('table', function($table){});
// 重命名数据表的列
$table->renameColumn('from', 'to');
// 移除指定的数据表列
$table->dropColumn(string|array);
// 指定数据表使用的存储引擎
$table->engine = 'InnoDB';
// 字段顺序,只能在 MySQL 中才能用
$table->string('name')->after('email');

索引

$table->string('column')->unique();
$table->primary('column');
// 创建一个双主键
$table->primary(array('first', 'last'));
$table->unique('column');
$table->unique('column', 'key_name');
// 创建一个双唯一性索引
$table->unique(array('first', 'last'));
$table->unique(array('first', 'last'), 'key_name');
$table->index('column');
$table->index('column', 'key_name');
// 创建一个双索引
$table->index(array('first', 'last'));
$table->index(array('first', 'last'), 'key_name');
$table->dropPrimary(array('column'));
$table->dropPrimary('table_column_primary');
$table->dropUnique(array('column'));
$table->dropUnique('table_column_unique');
$table->dropIndex(array('column'));
$table->dropIndex('table_column_index');

外键

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'|'restrict'|'set null'|'no action');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade'|'restrict'|'set null'|'no action');
$table->dropForeign(array('user_id'));
$table->dropForeign('posts_user_id_foreign');

字段类型

// 自增
$table->increments('id');
$table->bigIncrements('id');

// 数字
$table->integer('votes');
$table->tinyInteger('votes');
$table->smallInteger('votes');
$table->mediumInteger('votes');
$table->bigInteger('votes');
$table->float('amount');
$table->double('column', 15, 8);
$table->decimal('amount', 5, 2);

// 字符串和文本
$table->char('name', 4);
$table->string('email');
$table->string('name', 100);
$table->text('description');
$table->mediumText('description');
$table->longText('description');

// 日期和时间
$table->date('created_at');
$table->dateTime('created_at');
$table->time('sunrise');
$table->timestamp('added_on');
// Adds created_at and updated_at columns
 // 添加 created_at 和 updated_at 行
$table->timestamps();
$table->nullableTimestamps();

// 其它类型
$table->binary('data');
$table->boolean('confirmed');
// 为软删除添加 deleted_at 字段
$table->softDeletes();
$table->enum('choices', array('foo', 'bar'));
// 添加 remember_token 为 VARCHAR(100) NULL
$table->rememberToken();
// 添加整型的 parent_id 和字符串类型的 parent_type
$table->morphs('parent');
->nullable()
->default($value)
->unsigned()

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~