Artisan

Other topics

Introduction

Artisan is a utility that can help you do specific repetitive tasks with bash commands. It covers many tasks, including: working with database migrations and seeding, clearing cache, creating necessary files for Authentication setup, making new controllers, models, event classes, and a lot more.

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application.

To view a list of all available Artisan commands, you may use the list command:

php artisan list

To know more about the any available command, just precede its name with help keyword:

php artisan help [command-name]

List all registered routes filtered by multiple methods

php artisan route:list --method=GET --method=POST

This will include all routes that accept GET and POST methods simultaneously.

Running Laravel Artisan commands using PHP code

You can also use Laravel Artisan commands from your routes or controllers.

To run a command using PHP code:

Artisan::call('command-name');

For example,

Artisan::call('db:seed');

Creating and registering new artisan command

You can create new commands via

php artisan make:command [commandName]

So this will create [commandName] command class inside app/Console/Commands directory.

inside this class you will find protected $signature and protected $description variables, it represents name and discription of your command which will be used to describe your command.

after creating command you can register your command inside app/Console/Kernel.php class where you will find commands property.

so you can add your command inside the $command array like :

protected $commands = [
    Commands\[commandName]::class
];

and then i can use my command via console.

so as example i have named my command like

protected $signature = 'test:command';

So whenever i will run

php artisan test:command

it will call the handle method inside the class having signature test:command.

Syntax:

  • php artisan [command] [options] [arguments]

Parameters:

CommandDescription
clear-compiledRemove the compiled class file
downPut the application into maintenance mode
envDisplay the current framework environment
helpDisplays help for a command
listLists commands
migrateRun the database migrations
optimizeOptimize the framework for better performance
serveServe the application on the PHP development server
tinkerInteract with your application
upBring the application out of maintenance mode
app:nameSet the application namespace
auth:clear-resetsFlush expired password reset tokens
cache:clearFlush the application cache
cache:tableCreate a migration for the cache database table
config:cacheCreate a cache file for faster configuration loading
config:clearRemove the configuration cache file
db:seedSeed the database with records
event:generateGenerate the missing events and listeners based on registration
key:generateSet the application key
make:authScaffold basic login and registration views and routes
make:consoleCreate a new Artisan command
make:controllerCreate a new controller class
make:eventCreate a new event class
make:jobCreate a new job class
make:listenerCreate a new event listener class
make:middlewareCreate a new middleware class
make:migrationCreate a new migration file
make:modelCreate a new Eloquent model class
make:policyCreate a new policy class
make:providerCreate a new service provider class
make:requestCreate a new form request class
make:seederCreate a new seeder class
make:testCreate a new test class
migrate:installCreate the migration repository
migrate:refreshReset and re-run all migrations
migrate:resetRollback all database migrations
migrate:rollbackRollback the last database migration
migrate:statusShow the status of each migration
queue:failedList all of the failed queue jobs
queue:failed-tableCreate a migration for the failed queue jobs database table
queue:flushFlush all of the failed queue jobs
queue:forgetDelete a failed queue job
queue:listenListen to a given queue
queue:restartRestart queue worker daemons after their current job
queue:retryRetry a failed queue job
queue:tableCreate a migration for the queue jobs database table
queue:workProcess the next job on a queue
route:cacheCreate a route cache file for faster route registration
route:clearRemove the route cache file
route:listList all registered routes
schedule:runRun the scheduled commands
session:tableCreate a migration for the session database table
vendor:publishPublish any publishable assets from vendor packages
view:clearClear all compiled view files

Contributors

Topic Id: 1140

Example Ids: 3673,5503,25575,26387

This site is not affiliated with any of the contributors.