PHPStorm编写简单的TODOSAPP
一.创建工程
1.创建名为todos的工程
等待大约几分钟;
创建成功。
二. 准备工作
1.打开phpstorm,打开先前所创的todos工程(文件位置在laragon文件目录下的www中)
2.打开终端,进入刚才所创的todos的目录下
点击界面中的终端
3.利用artisan命令创建模型
php artisan make:Model Todo
创建成功
4.打开工程中的.env文件配置数据库的密码
根据自己数据库的情况配置密码
4.利用migrate创建数据库迁徙文件
php artisan make:migration create_todos_table
5.在迁徙文件中创建表字段
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTodosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('todos', function (Blueprint $table) {
$table->id();
$table->string('name',200);
$table->text('description');
//$table->string('picture');
$table->boolean('is_completed')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('todos');
}
}
6.执行数据库迁徙
php artisan migrate
7.利用Seeder和Factory填充数据
创建TodoFactory生成伪数据:
artisan make:factory TodoFactory -m Todo
加入下列代码
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Todo;
use Faker\Generator as Faker;
$factory->define(Todo::class, function (Faker $faker) {
return [
//
'name'=>$faker->sentence(3),
'description'=>$faker->paragraph(4),
// 'picture'=>$faker->imageUrl('128',128,'food'),
'is_completed'=>$faker->boolean(50)
];
});
8.创建TodoSeeder填充数据并在其中引用factory生成15条伪数据
php artisan make:seeder TodoSeeder