{"id":341,"date":"2023-01-29T19:02:39","date_gmt":"2023-01-29T19:02:39","guid":{"rendered":"https:\/\/mouryasolutions.in\/blog\/?p=341"},"modified":"2023-01-29T19:03:36","modified_gmt":"2023-01-29T19:03:36","slug":"call-artisan-command-from-route-in-laravel","status":"publish","type":"post","link":"https:\/\/mouryasolutions.in\/blog\/call-artisan-command-from-route-in-laravel\/","title":{"rendered":"How to call an Artisan command from a route In Laravel"},"content":{"rendered":"<div class=\"post-content\">\n<blockquote class=\"wp-block-quote\">\n<p>In Laravel, you can call an Artisan command from a route using the <code>Artisan<\/code> facade. Here is an example of how to do this:<\/p>\n<\/blockquote>\n\n\n\n<h4>Create an Artisan command using the <code>make:command<\/code> Artisan command:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:command ExampleCommand\r<\/code><\/pre>\n\n\n\n<h4>In the newly created <code>ExampleCommand<\/code> class, define the logic for the command.<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class ExampleCommand extends Command\r\n{\r\n    protected $signature = 'example:run';\r\n    protected $description = 'Run the example command';\r\n\r\n    public function handle()\r\n    {\r\n        $this->info('Example command run successfully!');\r\n    }\r\n}\r\n<\/code><\/pre>\n\n\n\n<h4>In your <code>routes\/web.php<\/code> file, define a route that calls the Artisan command:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Route::get('example', function () {\r\n    \\Artisan::call('example:run');\r\n    return 'Example command run successfully!';\r\n});\r\n<\/code><\/pre>\n\n\n\n<p>Now, when you visit <code>http:\/\/your-app.test\/example<\/code> in your browser, the Artisan command <code>example:run<\/code> will be executed and you will see the message &#8220;Example command run successfully!&#8221; displayed.<\/p>\n\n\n\n<p>Note: Replace <code>your-app.test<\/code> with the actual URL of your Laravel application.<\/p>\n<\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In Laravel, you can call an Artisan command from a<a href=\"https:\/\/mouryasolutions.in\/blog\/call-artisan-command-from-route-in-laravel\/\">Read More<i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"><\/i><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/341"}],"collection":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=341"}],"version-history":[{"count":2,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/341\/revisions\/505"}],"wp:attachment":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}