1.Call Artisan Command from Command Line
You can call your artisan command from command line like :
You can call your artisan command from command line like :
php artisan cache:clear
- Call Artisan Command from Controller
- routes.php
Add this get method in your routing file.
Route::get(‘test’, ‘TestController@test’); Now Create a controller file name as “TestController.php” under “app\Http\Controllers”.
Now when you call the test method like /test in your browser then the Artisan command will fire.<?phpnamespaceApp\Http\Controllers;useApp\Http\Controllers\Controller;useArtisan;classTestControllerextendsController {publicfunctiontest(){Artisan::call('cache:clear');}}?>
You can call your artisan command from route file as well.
routes.php
Route::get(‘/my_artisan’, function () {Artisan::call(“cache:clear”););});
Now you can execute the above route from your browser like /my_artisan
Now we will discuss all Artisan command one by one. First of all, to see what commands are available in laravel we will execute below command:
Which will output all available CLI commandsRoute::get('/my_artisan',function() {Artisan::call("list");dd(Artisan::output());});
clear-compiledUsage:\ncommand [options] [arguments]\n\nOptions:\n-h, --help Display this help message\n-q, --quiet Do not output any message\n-V, --version Display this application version\n--ansi Force ANSI output\n--no-ansi Disable ANSI output\n-n, --no-interaction Do not ask any interactive question\n--env[=ENV] The environment the command should run under.\n-v|vv|vvv, --verbose Increase the verbosity of messages: 1fornormal output, 2formore verbose outputand3fordebug\n\nAvailable commands:\nclear-compiled Remove the compiledclassfile\ndown Put the application into maintenance mode\nenv Display the current framework environment\nhelp Displays helpfora command\ninspire Display an inspiring quote\nlist Lists commands\nmigrate Run the database migrations\noptimize Optimize the frameworkforbetter performance\nserve Serve the application on the PHP development server\ntinker Interact with your application\nup Bring the application out of maintenance mode\napp\napp:name Set the applicationnamespace\nauth\nauth:clear-resetsFlushexpired password reset tokens\ncache\ncache:clearFlushthe application cache\ncache:table Create a migrationforthe cache database table\nconfig\nconfig:cache Create a cache fileforfaster configuration loading\nconfig:clear Remove the configuration cache file\ndb\ndb:seed Seed the database with records\nevent\nevent:generate Generate the missing eventsandlisteners based on registration\nhandler\nhandler:command Create anewcommand handlerclass\nhandler:event Create anewevent handlerclass\nkey\nkey:generate Set the application key\nmake\nmake:command Create anewcommandclass\nmake:console Create anewArtisan command\nmake:controller Create anewresource controllerclass\nmake:event Create aneweventclass\nmake:job Create anewjobclass\nmake:listener Create anewevent listenerclass\nmake:middleware Create anewmiddlewareclass\nmake:migration Create anewmigration file\nmake:model Create anewEloquent modelclass\nmake:provider Create anewservice providerclass\nmake:request Create anewform requestclass\nmake:seeder Create anewseederclass\nmigrate\nmigrate:install Create the migration repository\nmigrate:refresh Resetandre-run all migrations\nmigrate:reset Rollback all database migrations\nmigrate:rollback Rollback the last database migration\nmigrate:status Show the status of each migration\nqueue\nqueue:failed List all of the failed queue jobs\nqueue:failed-table Create a migrationforthe failed queue jobs database table\nqueue:flushFlushall of the failed queue jobs\nqueue:forgetDeletea failed queue job\nqueue:listen Listen to a given queue\nqueue:restart Restart queue worker daemons after their current job\nqueue:retry Retry a failed queue job\nqueue:subscribe Subscribe a URL to an Iron.io push queue\nqueue:table Create a migrationforthe queue jobs database table\nqueue:work Process the next job on a queue\nroute\nroute:cache Create a route cache fileforfaster route registration\nroute:clear Remove the route cache file\nroute:list List all registered routes\nschedule\nschedule:run Run the scheduled commands\nsession\nsession:table Create a migrationforthe session database table\nvendor\nvendor:publish Publish any publishable assets from vendor packages\nview\nview:clear Clear all compiled view files\n
This command remove the compiled class file from “/bootstrap/cache” folder.Route::get('/my_artisan',function() {Artisan::call('clear-compiled');});
clear-compiled
This command put your application into maintenance mode and it creates a file name as “down” under “/storage/framework” folder.Route::get('/my_artisan',function() {Artisan::call('down');});
env
It displays your current application environment like “local” . This function read the “APP_ENV” variable’s value from .env file which is located in here “/.env”Route::get('/my_artisan',function() {Artisan::call('env');});
help
If you want to take help for a command then you can use it. Which returns below result:Route::get('/my_artisan',function() {Artisan::call('help');dd(Artisan::output())});
inspireUsage:\nhelp [options] [--] [<command_name>]\n\nArguments:\ncommand The command to execute\ncommand_name The command name [default:"help"]\n\nOptions:\n--xml To output helpasXML\n--format=FORMAT The output format (txt, xml, json,ormd) [default:"txt"]\n--raw To output raw command help\n-h, --help Display this help message\n-q, --quiet Do not output any message\n-V, --version Display this application version\n--ansi Force ANSI output\n--no-ansi Disable ANSI output\n-n, --no-interaction Do not ask any interactive question\n--env[=ENV] The environment the command should run under.\n-v|vv|vvv, --verbose Increase the verbosity of messages: 1fornormal output, 2formore verbose outputand3fordebug\n\nHelp:\nThe help command displays helpfora given command:\n\nphp /cs/public/index.php help list\n\nYou can also output the help in other formats by using the --format option:\n\nphp /cs/public/index.php help --format=xml list\n\nTo display the list of available commands, pleaseusethe list command.\n
This function display an inspiring quotes.Route::get('/my_artisan',function() {Artisan::call('inspire');dd(Artisan::output())});
list
Display all available commands.Earlier I have already discussed about this command.Route::get('/my_artisan',function() {Artisan::call('list');dd(Artisan::output())});
optimize
This function creates 2 files (compiled.php and services.json) under \bootstrap\cache folder which is used for better performance.Route::get('/my_artisan',function() {Artisan::call('optimize',['--force'=>'force']);});
serve
This function will serve the application on the PHP development server.Route::get('/my_artisan',function() {Artisan::call("serve",["--host"=>'127.0.0.1']);});
up
You can’t use this function from controller or route. You must execute from command line to make it live.
This command makes your application live from maintenance mode.php artisan up
app:name
This function set your application namespace.Route::get('/my_artisan',function() { (Artisan::call("app:name",['name'=>'TestApp']));});
auth:clear-resets
This command deletes expired password tokens which is less than one hour from current time, from password_resets table.Route::get('/my_artisan',function() { (Artisan::call("auth:clear-resets"));});
cache:clear
Deletes application cache from “\storage\framework\cache” folder.Route::get('/my_artisan',function() {Artisan::call("cache:clear");});
cache:clear
This command creates a migration for the cache table under this path “\database\migrations\2016_06_10_112327_create_cache_table.php”Route::get('/my_artisan',function() { (Artisan::call("cache:table"));});
The filename is create according to current date and time like this format: YYYY_mm_dd_His_create_cache_table.php
config:cache
This function creates a cache file for faster configuration loading and it creates a “cache.php” file under “\bootstrap\cache\” folder.Route::get('/my_artisan',function() { (Artisan::call("config:cache"));});
config:clear
Remove configuration file which created by “config:cache” commandRoute::get('/my_artisan',function() { (Artisan::call("config:clear"));});
event:generate
This function generates the missing events and listeners based on registration which is defined in “\App\Providers\EventServiceProvider.php” files like below :Route::get('/my_artisan',function() {Artisan::call("event:generate");});
Above command will genaretes 1 Events file under “\App\Events\SomeEvent.php” and 1 Listners file under “\App\Listeners\EventListener.php”protected$listen= ['App\Events\SomeEvent'=> ['App\Listeners\EventListener',],];
handler:command
This function creates a new command handler class under “\app\Handlers\Commands\TestCommand.php”Route::get('/my_artisan',function() {Artisan::call("handler:command",['name'=>'TestCommand']);});
handler:event
Create a new command handler class under “\app\Handlers\Events\TestEvent.php”Route::get('/my_artisan',function() {Artisan::call("handler:event",['name'=>'TestEvent']);});
key:generate
Generates a randomly application key and set the value in APP_KEY in .env file.Route::get('/my_artisan',function() { (Artisan::call("key:generate"));});
make:command
Create a new command Class under “\app\Commands\TestCommand.php”Route::get('/my_artisan',function() { (Artisan::call("make:command",['name'=>'TestCommand']));});
make:console
Create a new Artisan command under “\app\Console\Commands\TestConsole.php”;Route::get('/my_artisan',function() { (Artisan::call("make:console",['name'=>'TestConsole']));});
make:controller
Create a new resource controller Class under “\app\Http\Controllers\TestController.php”Route::get('/my_artisan',function() { (Artisan::call("make:controller",['name'=>'TestController']));});
make:event
Create a new event Class under “\app\Events\TestEvent.php”Route::get('/my_artisan',function() { (Artisan::call("make:event",['name'=>'TestEvent']));});
make:job
Create a new job Class under “\app\Jobs\TestJob.php”Route::get('/my_artisan',function() { (Artisan::call("make:job",['name'=>'TestJob']));});
make:listener
Create a new event listner Class under “\app\Listeners\TestListener.php”Route::get('/my_artisan',function() { (Artisan::call("make:listener",['name'=>'TestListener']));});
make:middleware
Create a new middleware Class under “\app\Http\Middleware\TestMiddleware.php”Route::get('/my_artisan',function() { (Artisan::call("make:middleware",['name'=>'TestMiddleware']));});
make:migration
Create a new migration file under “\database\migrations\2016_06_11_110210_TestMigration.php”Route::get('/my_artisan',function() { (Artisan::call("make:migration",['name'=>'TestMigration']));});
make:model
Create a new Eloquent Model Class under “\app\TestModel.php”Route::get('/my_artisan',function() { (Artisan::call("make:model",['name'=>'TestModel']));});
make:provider
Create a new service provider Class under “\app\Providers\TestProviders.php”Route::get('/my_artisan',function() { (Artisan::call("make:provider",['name'=>'TestProviders']));});
make:request
Create a new form request Class under “\app\Http\Requests\TestRequests.php”Route::get('/my_artisan',function() { (Artisan::call("make:request",['name'=>'TestRequests']));});
make:seeder
Create a new seeder Class under “\database\seeds\TestSeeder.php” By default it generates a single method name “run()”.Route::get('/my_artisan',function() { (Artisan::call("make:seeder",['name'=>'TestSeeder']));});
How to use migrate command?
migrate
Run the database migrationsRoute::get('/my_artisan,function() { (Artisan::call("migrate"));});
migrate:install
Create the migration repository table name as “migrations” in your database.Route::get('/my_artisan,function() { (Artisan::call("migrate:install"));});
migrate:refresh
Reset and re-run all migration. It means it creates all the table which is under this “\database\migrations”Route::get('/my_artisan',function() { (Artisan::call("migrate:refresh"));});
migrate:reset
Rollback all database migration. It means it deletes all the tables from your database except migration table (But table structure will remain only).Route::get('/my_artisan',function() { (Artisan::call("migrate:reset"));});
migrate:rollback
Rollback the last database migration. It means it executes the last command which you have used recently.Route::get('/my_artisan',function() { (Artisan::call("migrate:rollback"));});
migrate:status
Above function shows the status of each migration like below:Route::get('/my_artisan',function() {Artisan::call("migrate:status");dd(Artisan::output());});
queue:failed+------+------------------------------------------------+\n| Ran? | Migration |\n+------+------------------------------------------------+\n| N | 2014_10_12_000000_create_users_table |\n| N | 2014_10_12_100000_create_password_resets_table |\n| N | 2016_04_02_131806_create_jobs_table |\n| N | 2016_06_10_112609_create_cache_table |\n+------+------------------------------------------------+\n
List all of the failed queue jobs from “failed_jobs” Table. Result will look like below :Route::get('/my_artisan',function() {Artisan::call('queue:failed');});
queue:failed-table+----+------------+---------+---------------------+-----------+\n| ID | Connection | Queue | Class | Failed At |\n+----+------------+---------+---------------------+-----------+\n| 1 | Test | Testing | 2016-06-21 13:19:37 | |\n+----+------------+---------+---------------------+-----------+\n
Create a migration for the failed queue jobs database table under “\database\migrations\2016_06_14_103032_create_failed_jobs_table.php”Route::get('/my_artisan',function() {Artisan::call('queue:failed-table');});
queue:flush
Delete all failed jobs from failed_jobs TableRoute::get('/my_artisan',function() {Artisan::call('queue:flush');});
queue:forget
Delete a failed queue job from failed_jobs TableRoute::get('/my_artisan',function() { (Artisan::call("queue:forget",['id'=>'2']));});
queue:restart
Restart queue worker daemons after their current jobRoute::get('/my_artisan',function() {Artisan::call('queue:restart');});
queue:table
create a migration for the queue jobs database table under “\database\migrations\2016_06_14_121326_create_jobs_table.php”Route::get('/my_artisan',function() {Artisan::call('queue:table');});
How to use Route Artisan Command
route:cache
If you run above command then might be you get following error :Route::get('/my_artisan',function() {Artisan::call("route:cache");});
Unable to prepare route [/] for serialization. Uses Closure.
It means you can use Closure only in your route file.
You can call this artisan from from either controller’s method or from command line:
It creates a route cache file for faster route registration under “\bootstrap\cache\routes.php”Controller : Artisan::call("route:cache");CLI : php artisan route:cache
route:clear
Again you need to remove from either controller’s method or from command line. This command remove the route cache file.Route::get('/my_artisan',function() { (Artisan::call("route:clear"));});
route:clear
This function display your all registered routes. The output will look like below:Route::get('/my_artisan',function() {Artisan::call("route:list");dd(Artisan::output());});
route:clear+--------+----------+------------+------+--------------------------------------------------+------------+\n| Domain | Method | URI | Name | Action | Middleware |\n+--------+----------+------------+------+--------------------------------------------------+------------+\n| | GET|HEAD | / | | Closure | |\n| | GET|HEAD | foo | | Closure | |\n| | GET|HEAD | register | | App\Http\Controllers\UserController@register | |\n| | POST | save_user | | App\Http\Controllers\UserController@SaveRegister | |\n| | GET|HEAD | users/{id} | | Closure | |\n+--------+----------+------------+------+--------------------------------------------------+------------+\n
Run the scheduled commandsRoute::get('/my_artisan',function() {Artisan::call("schedule:run");});
session:table
Create a migration for the session database table under “\database\migrations\2016_06_16_131413_create_session_table.php”Route::get('/my_artisan',function() {Artisan::call("session:table");});
view:clear
Clear all view compiled files from “\storage\framework\views” folderRoute::get('/my_artisan',function() {Artisan::call("view:clear");});
Keep in touch for more tutorials.

