Artisan 命令
1. Artisan简介
- Artisan是Laravel中自带的命令行工具的名称
- 由强大的Symfony consle 组件驱动的
- 提供来一些对应用开发有帮助的命令
2. Artisan使用帮助
1. 查看所有可用的Artisan的命令(list)
php artisan
php artisan list
2. 查看命令的帮助信息(help)
php artisan help migrate
3. Artisan基本使用
1. 创建控制器
php artisan make:controller StudentController
2. 创建模型
php artisan make:model Student
指定路径创建
php artisan make:model Models/Activity
3. 创建中间件
php artisan make:middleware Activity
4. Laravel中的用户认证 (Auth)
- 生成Auth所需文件
- 数据迁移
- 数据填充
1. 生成Auth所需文件
在Artisan控制台输入以下命令
php artisan make:auth
vagrant@homestead:~/code/Laravel$ php artisan make:auth
Authentication scaffolding generated successfully.
产生影响:
//在routes/web.php文件中
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
//在resources/views目录下生成
auth文件夹
layouts文件夹
//在vendor/laravel/framework/src/Illuminate/Routing/Router.php生成对应文件
数据迁移命令php artisan migrate
vagrant@homestead:~/code/Laravel$ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
5. 新建迁移文件
-
新建一个students表的迁移文件
php artisan make:migration create_student_table
—table 和 —create参数可以用来指定数据表名称,以及迁移文件是否要建立新的数据表
-
生成模型的同时生成迁移文件
php artisan make:model Student -m
实践操作1:
//新建一个迁移文件
php artisan make:migration create_student_table --create=students
//执行后
vagrant@homestead:~/code/Laravel$ php artisan make:migration create_student_table --create=students
Created Migration: 2019_05_28_221527_create_student_table
//在database/migrations生成文件2019_05_28_221527_create_student_table.php
文件具体
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStudentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('students');
}
}
实践操作2:
创建model同时创建迁移文件
vagrant@homestead:~/code/Laravel$ php artisan make:model Models/Article -m
Model created successfully.
Created Migration: 2019_05_28_221915_create_articles_table
修改2019_05_28_221527_create_student_table.php
文件
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('age')->unsigned()->default(0);
$table->integer('sex')->unsigned()->default(0);
$table->integer('created_at')->default(0);
$table->integer('updated_at')->default(0);
});
}
然后执行php artisan migrate
vagrant@homestead:~/code/Laravel$ php artisan migrate
Migrating: 2019_05_28_221527_create_student_table
Migrated: 2019_05_28_221527_create_student_table
Migrating: 2019_05_28_221915_create_articles_table
Migrated: 2019_05_28_221915_create_articles_table
表就创建好了
6. 数据填充
-
创建一个填充文件,并完善填充文件
php artisan make:seeder StudentTableSeeder
-
执行单个填充文件
php artisan db:seed --class=StudentTableSeeder
-
批量执行填充文件
php artisan db:seed
//创建一个填充文件,并完善填充文件
vagrant@homestead:~/code/Laravel$ php artisan make:seeder StudentTableSeeder
Seeder created successfully.
//生成文件database/seeds/StudentTableSeeder.php
<?php
use Illuminate\Database\Seeder;
class StudentTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}
//完善方法
<?php
use Illuminate\Database\Seeder;
class StudentTableSeeder extends Seeder
{
public function run()
{
//
\Illuminate\Support\Facades\DB::table('students')->insert([
['name'=>'care','age'=>12],
['name'=>'winnie','age'=>12],
]);
}
}
//执行命令,单个填充文件
php artisan db:seed --class=StudentTableSeeder
//数据填充成功
//批量填充
public function run()
{
$this->call(StudentTableSeeder::class);
}
//执行命令
php artisan db:seed
本作品采用《CC 协议》,转载必须注明作者和本文链接