<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sachin Kiranti on Medium]]></title>
        <description><![CDATA[Stories by Sachin Kiranti on Medium]]></description>
        <link>https://medium.com/@sachinkiranti?source=rss-f0cb1eae7f50------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Q6MKF1WYQY_sjVXctg_gaw.jpeg</url>
            <title>Stories by Sachin Kiranti on Medium</title>
            <link>https://medium.com/@sachinkiranti?source=rss-f0cb1eae7f50------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:31:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sachinkiranti/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Writing your own custom Laravel Artisan Command]]></title>
            <link>https://medium.com/@sachinkiranti/writing-your-own-custom-laravel-artisan-command-571f823cf42?source=rss-f0cb1eae7f50------2</link>
            <guid isPermaLink="false">https://medium.com/p/571f823cf42</guid>
            <category><![CDATA[artisan]]></category>
            <category><![CDATA[command-line-interface]]></category>
            <category><![CDATA[commands]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Sachin Kiranti]]></dc:creator>
            <pubDate>Sun, 30 Sep 2018 14:40:49 GMT</pubDate>
            <atom:updated>2018-11-23T05:22:40.035Z</atom:updated>
            <content:encoded><![CDATA[<p>We have been using many artisan commands in our day to day work . Laravel provides many artisan commands to make our life easier . From creating model to controller , request and many other with options for generating related materials like</p><p><strong>php artisan make:model Hello -mcr</strong></p><p>Above commands will generate model, migration, resource controller for your model <strong>Hello</strong> .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*iYEhuMhMlD7xc9FP_y_CLw.png" /><figcaption>Generating model, migration and resource controller</figcaption></figure><p>Easy-peasy right ? 🙂 With a line of command we have our <strong>model</strong>, <strong>migration</strong> and <strong>resource controller</strong> ready . There are many other helpful commands which <strong>Laravel</strong> provides out of the box for you . You can list out all the commands using :</p><p><strong>php artisan list</strong></p><p>In addition to that, you can found many plugins out there to extend your artisan commands experience 🙂.</p><p>Today , we will be <strong>creating a simple custom artisan command which will clean up our cache, route, view with a line of command </strong>. We do have all the mention features already in laravel , we will be utilizing that in our artisan command . Let’s get started . 🙂</p><h3>Getting Started</h3><p>First, let’s hit a command to generate the blueprint for our artisan command . Enter the below command on your terminal .</p><p><strong>php artisan make:command CleanUpCommand</strong></p><p>Now , you will find your command on <strong>app/console/commands/</strong> . We will start coding our artisan command right away but first please do read <strong>Laravel</strong> documentation about creating commands . I won’t be summarizing how exactly does artisan command works. This is all well documented on <a href="https://laravel.com/docs/5.7/artisan"><strong>Laravel official website</strong></a> .</p><p>We will be using bunch of already made commands like :</p><ul><li>php artisan view:clear</li><li>php artisan route:clear</li><li>php artisan cache:clear</li><li>php artisan config:clear</li><li>php artisan clear-compiled</li></ul><p>First let’s declare a protected variable with arrays of commands .</p><pre>/**</pre><pre>* List of artisan commands</pre><pre>* @var array</pre><pre>*/</pre><pre>protected $commands = [ &#39;view:clear&#39;, &#39;cache:clear&#39;, &#39;config:clear&#39;, &#39;route:clear&#39;, &#39;clear-compiled&#39; ];</pre><p>After that, let’s inject our composer on our constructor .</p><pre>/**</pre><pre>* The Composer instance.</pre><pre>* @var \Illuminate\Support\Composer</pre><pre>*/</pre><pre>protected $composer;</pre><pre>/**</pre><pre>* Create a new command instance.</pre><pre>* @param Composer $composer</pre><pre>* @param Artisan $artisan</pre><pre>*/</pre><pre>public function __construct(Composer $composer)</pre><pre>{</pre><pre>parent::__construct();</pre><pre>$this-&gt;composer = $composer;</pre><pre>}</pre><p>After initializing the composer , let’s start the main functionality we were talking about .</p><pre>/**</pre><pre>* Execute the console command.</pre><pre>* @return void</pre><pre>*/</pre><pre>public function handle()</pre><pre>{</pre><pre>// First </pre><pre>$this-&gt;composer-&gt;dumpAutoloads();</pre><pre>$this-&gt;composer-&gt;dumpOptimized();</pre><pre>// Second</pre><pre>$outputs = [];</pre><pre>foreach ($this-&gt;commands as $command) :</pre><pre>$this-&gt;call($command);</pre><pre>endforeach;</pre><pre>// Third</pre><pre>if (function_exists(&#39;exec&#39;)) :</pre><pre>exec(&#39;truncate -s 0 storage/logs/laravel.log&#39;, $output, $result);</pre><pre>if (!$result) {</pre><pre>$outputs[] = &#39;Log cleared successfully!&#39;;</pre><pre>} else {</pre><pre>$outputs[] = &#39;Log clear failed!&#39;;</pre><pre>}</pre><pre>else :</pre><pre>$outputs[] = &#39;Log clear command cannot be init! exec is disabled!&#39;;</pre><pre>endif;</pre><pre>$this-&gt;info(implode(&#39;&#39;, $outputs));</pre><pre>$this-&gt;info(&#39;All done successfully ! Love kiranti :) &#39;);</pre><pre>}</pre><p>We have written complete function for our command . The first two line of code will do the job like :</p><p><strong>composer dumpautoload</strong></p><p>In second line of code , we are just calling the artisan commands for each item from our array from the class .</p><p><strong>php artisan view:clear</strong></p><p>and so on .</p><p>In the third part of code , we are using <strong>exec()</strong> function to execute the external command which will truncate our log file .</p><p>At the end , we are imploding the output (messages) . The final thing to do is give a signature and description for our command .</p><pre>/**</pre><pre>* The name and signature of the console command.</pre><pre>* @var string</pre><pre>*/</pre><pre>protected $signature = &#39;kiranti:cleanup&#39;;</pre><pre>/**</pre><pre>* The console command description.</pre><pre>* @var string</pre><pre>*/</pre><pre>protected $description = &#39;Clean up all views, cache and dump composer&#39;;</pre><p>Our command is ready . All we need is to register our commands on <strong>App\Console\kernel.php</strong> file . We just need to add our command in <strong>protected $commands</strong> array .</p><pre>/**</pre><pre>* The Artisan commands provided by your application.</pre><pre>* @var array</pre><pre>*/</pre><pre>protected $commands = [</pre><pre>CleanUpCommand::class</pre><pre>];</pre><h3>The final code :</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d98f45b0e124dc7043c1868ba45bb453/href">https://medium.com/media/d98f45b0e124dc7043c1868ba45bb453/href</a></iframe><p>Finally , we have our command ready 😍. If you run<strong> php artisan list</strong> , we will see our command listed there .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*6UtttldrQGhGfwUggY76KQ.png" /><figcaption>Our command on the artisan list</figcaption></figure><p>Let’s run our command 🙂 and test it out .</p><p><strong>php artisan kiranti:cleanup</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*OwfzpoQ-UABdYnykaXqJWw.png" /><figcaption>php artisan kiranti:cleanup</figcaption></figure><h3>Conclusion</h3><p>This is just a simple demonstration for a command but you can play around and make something useful . We have covered a lot of topic in this article . Hope this help you understand commands and let you create your own . Happy coding 😍</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=571f823cf42" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>