学习使用laravel的artisan创建需要的文件
控制器:
php artisan make:controller Test/DemoController
模型:
php artisan make:model Models/Demo
使用laravel自带数据库控制流程
1.使用 artisan 生成 Migration
php artisan make:migration create_表名_table
例子:
public function up()
{
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->text('body')->nullable();
$table->integer('user_id');
$table->timestamps();
});
}
2.进行数据库迁移(migration)
运行命令:
php artisan migrate
3.添加测试数据,创建Seeder文件
php artisan make:seeder ArticleSeeder
例子:
public function run()
{
DB::table('articles')->delete();
for ($i=0; $i < 10; $i++) {
\App\Article::create([
'title' => 'Title '.$i,
'body' => 'Body '.$i,
'user_id' => 1,
]);
}
}
接下来我们把 ArticleSeeder 注册到系统内。
修改 learnlaravel/database/seeds/DatabaseSeeder.php 中的 run 函数为:
public function run()
{
$this->call(ArticleSeeder::class);
}
再执行两个指令
composer dump-autoload
php artisan db:seed