举例说明
创建 cityss 表在mysql
在dos下,进入项目目录
d:\workout\
键入命令
d\workout\php arttsan make:migration create_citys_table
这个命令执行后
会在
d:\workout\database\migrations 下面生成一个新文件 yyyy_mm_dd_hhmmss_create_citys_table
打开这个文件
写入如下代码
public function up()
{
Schema::create('citys', function(Blueprint $table) {
$table->increments('id');
$table->string('code', 10)->nullable();
$table->string('name', 100)->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('citys');
}
保存之后
执行 d:\workout\php artisan migrate
显示 Migrate:yyyy_mm_dd_hhmmss_create_citys_table
这是你查看mysql,citys表已经创建好了。
添加记录
在项目中找到
\database\seed\DatabaseSeeder
在run()
中添加
City::create([
'code' => '024',
'name' => 'shenyang'
]);
这之前,要创建model文件
\app\models\City.php
内容如下
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class City extends Model {
public $timestamps = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'citys';
public function areas()
{
return $this->hasMany( 'App\Models\CityArea','cityCode','code' );
}
}
然后执行
d:\workout\php artisan db:seed
这个记录就插入到表格中了
这里遇到一个未解决的问题
如果把 shenyang 换成 沈阳
中文字符 插入不到DB中,相应的会变成空白
怎么解决还不知道,欢迎回复