Artisan 命令

1. Artisan简介

  1. Artisan是Laravel中自带的命令行工具的名称
  2. 由强大的Symfony consle 组件驱动的
  3. 提供来一些对应用开发有帮助的命令

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)

  1. 生成Auth所需文件
  2. 数据迁移
  3. 数据填充
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. 新建迁移文件

  1. 新建一个students表的迁移文件

    php artisan make:migration create_student_table

    —table 和 —create参数可以用来指定数据表名称,以及迁移文件是否要建立新的数据表

  2. 生成模型的同时生成迁移文件

    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. 数据填充

  1. 创建一个填充文件,并完善填充文件

    php artisan make:seeder StudentTableSeeder

  2. 执行单个填充文件

    php artisan db:seed --class=StudentTableSeeder

  3. 批量执行填充文件

    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 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!