PHP

Laravel Artisan 使用: 最佳实践与技巧

silverwq
2024-03-29 / 0 评论 / 60 阅读 / 正在检测是否收录...

系统命令使用

artisan 命令主要是控制台的命令,输入 php artisan 后输出

PS D:\project\php\strongshop> php artisan
Laravel Framework 6.20.30

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  clear-compiled               Remove the compiled class file
  down                         Put the application into maintenance mode
  env                          Display the current framework environment
  help                         Display help for a command
  list                         List commands
  migrate                      Run the database migrations
  optimize                     Cache the framework bootstrap files
  preset                       Swap the front-end scaffolding for the application
  serve                        Serve the application on the PHP development server
  tinker                       Interact with your application
  ui                           Swap the front-end scaffolding for the application
  up                           Bring the application out of maintenance mode
 auth
  auth:clear-resets            Flush expired password reset tokens
 cache
  cache:clear                  Flush the application cache
  cache:forget                 Remove an item from the cache
  cache:table                  Create a migration for the cache database table
 config
  config:cache                 Create a cache file for faster configuration loading
  config:clear                 Remove the configuration cache file
 db
  db:seed                      Seed the database with records
  db:wipe                      Drop all tables, views, and types
 event
  event:cache                  Discover and cache the application's events and listeners
  event:clear                  Clear all cached events and listeners
  event:generate               Generate the missing events and listeners based on registration
  event:list                   List the application's events and listeners
 key
  key:generate                 Set the application key
 make
  make:channel                 Create a new channel class
  make:command                 Create a new Artisan command
  make:controller              Create a new controller class
  make:event                   Create a new event class
  make:exception               Create a new custom exception class
  make:factory                 Create a new model factory
  make:job                     Create a new job class
  make:listener                Create a new event listener class
  make:mail                    Create a new email class
  make:middleware              Create a new middleware class
  make:migration               Create a new migration file
  make:model                   Create a new Eloquent model class
  make:notification            Create a new notification class
  make:observer                Create a new observer class
  make:policy                  Create a new policy class
  make:provider                Create a new service provider class
  make:request                 Create a new form request class
  make:resource                Create a new resource
  make:rule                    Create a new validation rule
  make:seeder                  Create a new seeder class
  make:test                    Create a new test class
 migrate
  migrate:fresh                Drop all tables and re-run all migrations
  migrate:install              Create the migration repository
  migrate:refresh              Reset and re-run all migrations
  migrate:reset                Rollback all database migrations
  migrate:rollback             Rollback the last database migration
  migrate:status               Show the status of each migration
 notifications
  notifications:table          Create a migration for the notifications table
 optimize
  optimize:clear               Remove the cached bootstrap files
 package
  package:discover             Rebuild the cached package manifest
 queue
  queue:failed                 List all of the failed queue jobs
  queue:failed-table           Create a migration for the failed queue jobs database table
  queue:flush                  Flush all of the failed queue jobs
  queue:forget                 Delete a failed queue job
  queue:listen                 Listen to a given queue
  queue:restart                Restart queue worker daemons after their current job
  queue:retry                  Retry a failed queue job
  queue:table                  Create a migration for the queue jobs database table
  queue:work                   Start processing jobs on the queue as a daemon
 route
  route:cache                  Create a route cache file for faster route registration
  route:clear                  Remove the route cache file
  route:list                   List all registered routes
 schedule
  schedule:run                 Run the scheduled commands
 scout
  scout:flush                  Flush all of the model's records from the index
  scout:import                 Import the given model into the search index
 session
  session:table                Create a migration for the session database table
 storage
  storage:link                 Create a symbolic link from "public/storage" to "storage/app/public"
 telescope
  telescope:clear              Clear all entries from Telescope
  telescope:install            Install all of the Telescope resources
  telescope:prune              Prune stale entries from the Telescope database
  telescope:publish            Publish all of the Telescope resources
 ui
  ui:auth                      Scaffold basic login and registration views and routes
 vendor
  vendor:publish               Publish any publishable assets from vendor packages
 view
  view:cache                   Compile all of the application's Blade templates
  view:clear                   Clear all compiled view files

查看所有命令

php artisan
php aratisan list

每个命令都有个帮助

例如

php artisan help migrate

运行迁移

执行未执行过的迁移文件,如果已经执行过不会被执行

php artisan migrate

此命令将执行/database/migrations目录下的所有未执行的迁移文件,主要是执行文件里的up方法。并将记录插入Laravel迁移表中。laravel 里有个 前缀_migrations 的表来判断是否已经执行过了

Make 创建命令

生成控制器

要创建一个控制器,可以使用以下命令:

php artisan make:controller UserController

其中UserController是控制器的名称。执行以上命令后,Laravel会在/app/Http/Controllers目录下生成一个UserController.php文件,该文件是一个空的控制器类。

生成模型

要创建一个模型,可以使用以下命令,laravel的model,没有Model后缀:

php artisan make:model User

其中User是模型的名称。执行以上命令后,Laravel会在/app目录下生成一个User.php文件,该文件是一个空的模型类。

自动生成注释

php artisan ide-helper:models "App\Models\User"   记住一定要加引号

生成迁移

php artisan make:migration create_user_table --create=users

其中create_users_table是迁移文件的名称,--create=users表示要创建一个名为users的表。执行以上命令后,Laravel会在/database/migrations目录下生成一个迁移文件,该文件包含up()和down()两个方法,可以在其中添加创建和删除表的操作。

luch4dxo.png

生成 seeder

php artisan make:seeder UsersTableSeeder

其中UsersTableSeeder是Seeder的名称。执行以上命令后,Laravel会在/database/seeds目录下生成一个UsersTableSeeder.php文件,该文件是一个类,可以在其中添加插入数据的操作

生成命令

要创建新命令,可以使用 make:command Artisan 命令。该命令会在 app/Console/Commands 目录下创建一个新的命令类。如果该目录不存在,也无需担心 - 它会在第一次运行 make:command Artisan 命令的时候自动创建:

php artisan make:command SendEmails

db 相关的操作

运行Seeder

php artisan db:seed

此命令将执行/database/seeds目录下的所有Seeder类,并将记录插入数据库中。试了下好像不行

注册command

  1. 类命令:在/app/Console/Commands目录下创建一个新的PHP类
  2. 闭包命令:routes/console.php

luchzqxs.png

命令还有一种是单例命令,可以确保没用同时执行的命令。

0

评论 (0)

取消