一、下载 laravel 5.1
composer create-project laravel/laravel=5.1.* laravel5.1_api
新建数据库 laravel5.1_api
修改 .evn 配置文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5.1_api
DB_USERNAME=laravel5.1_api
DB_PASSWORD=laravel5.1_api
修改中国时区,在 config/app.php 中修改
'timezone' => 'PRC',
切换目录
cd laravel5.1_api
二、创建数据
创建 lesson migration
php artisan make:migration create_lessons_table --create=lessons
创建 lesson model
php artisan make:model Lesson
创建 lessson controller
php artisan make:controller LessonsController
修改迁移文件:lessons_table.php
public function up()
{
Schema::create('lessons', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('content');
$table->boolean('free');
$table->timestamps();
});
}
执行数据迁移
php artisan migrate
如果报错
Specified key was too long; max key length is 1000 bytes
问题解决:在 AppServiceProvider 中调用 Schema::defaultStringLength 方法来实现配置:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
修改:database/factories/ModelFactory.php
$factory->define(App\Lesson::class, function (Faker\Generator $faker) {
return [
'title' => $faker->sentence,
'content' => $faker->paragraph,
'free' => $faker->boolean(),
];
});
进入 tinker
php artisan tinker
创建课程数据
factory('App\Lesson', 60)->create();