{"id":16698,"date":"2024-02-23T06:00:00","date_gmt":"2024-02-23T06:00:00","guid":{"rendered":"https:\/\/www.directimpactsolutions.com\/?p=16698"},"modified":"2025-05-01T02:54:37","modified_gmt":"2025-05-01T02:54:37","slug":"laravel-stubs","status":"publish","type":"post","link":"https:\/\/www.directimpactsolutions.com\/en\/laravel-stubs\/","title":{"rendered":"Laravel Stubs"},"content":{"rendered":"<p>Stubs are class templates that come with Laravel. They get used behind the scenes whenever you create new files within your project. Stubs are hidden from view by default, so I don\u2019t think they are appreciated enough.<\/p><p>Chances are, if you have worked with <a href=\"https:\/\/www.directimpactsolutions.com\/en\/laravel\/\">Laravel<\/a>, you\u2019ve used an <code>artisan make<\/code> command to make a new model, controller, or migration script. The act of doing so would have utilized a stub to create a new file.<\/p><p>Let\u2019s discuss how stubs work. Understanding their inner workings will help us make better use of them.<\/p><div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><nav><ul><li class=\"\"><a href=\"#an-example-stub\">An Example Stub<\/a><\/li><li class=\"\"><a href=\"#stubs-in-action\">Stubs In Action<\/a><\/li><li class=\"\"><a href=\"#come-out-come-out-wherever-you-are\">Come Out, Come Out, Wherever You Are!<\/a><\/li><li class=\"\"><a href=\"#so-many-stubs\">So Many Stubs<\/a><\/li><li class=\"\"><a href=\"#where-to-next\">Where To Next?<\/a><ul><li class=\"\"><a href=\"#naming-conventions-basic\">Naming Conventions (Basic)<\/a><\/li><li class=\"\"><a href=\"#simple-modifications-basic\">Simple Modifications (Basic)<\/a><\/li><li class=\"\"><a href=\"#new-workflows-intermediate\">New Workflows (Intermediate)<\/a><\/li><li class=\"\"><a href=\"#overriding-artisan-make-advanced\">Overriding Artisan Make (Advanced)<\/a><\/li><\/ul><\/li><li class=\"\"><a href=\"#wrapping-up\">Wrapping Up<\/a><ul><li class=\"\"><a href=\"#artisan-is-a-laravel-command-that-needs-to-be-executed-through-php-these-commands-can-be-run-using-php-artisan-if-being-used-locally-or-vendor-bin-sail-artisan-if-youre-using-laravel-sail\">*`artisan` is a Laravel command that needs to be executed through PHP. These commands can be run using php artisan if being used locally, or .\/vendor\/bin\/sail artisan if you\u2019re using Laravel Sail.<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div><h2 class=\"wp-block-heading\" id=\"an-example-stub\" style=\"font-style:normal;font-weight:400\">An Example Stub<\/h2><p>Since a picture tells a thousand words, let\u2019s use a simple stub that is included with Laravel as an example. This one is a template for making new models in your project. This particular stub is used when you invoke the <code>artisan make:model<\/code> command.<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"510\" height=\"307\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/laravel-stubs-1.png\" alt=\"laravel stubs template file\n\" class=\"wp-image-16699\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/laravel-stubs-1.png 510w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/laravel-stubs-1-300x181.png 300w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/figure><p>As you can see, most of it is plain PHP, and pretty simple.<br>The exceptions are <code>{{ namespace }}<\/code> and <code>{{ class }}<\/code>. These are template tags that will be replaced when Laravel creates a new file from your stub.<\/p><p>Those of you familiar with the Laravel <code>artisan make:model<\/code> command will know at least one more argument needs to be provided to the command: the name!<\/p><h2 class=\"wp-block-heading\" id=\"stubs-in-action\" style=\"font-style:normal;font-weight:400\">Stubs In Action<\/h2><p>When I run the command <code>artisan make:model Example<\/code> the system generates the following file for me:<\/p><figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"307\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/laravel-stubs-2.png\" alt=\"Laravel stubs\" class=\"wp-image-16702\" srcset=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/laravel-stubs-2.png 548w, https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/laravel-stubs-2-300x168.png 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/figure><p>You\u2019ll notice it mirrors the stub template file, but has replaced the template tags for both <code>namespace<\/code><\/p><p>and <code>class<\/code>. Laravel used the information we provided on the command line to not only choose an appropriate stub, but also to calculate the values for the template tags.<\/p><h2 class=\"wp-block-heading\" id=\"come-out-come-out-wherever-you-are\" style=\"font-style:normal;font-weight:400\">Come Out, Come Out, Wherever You Are!<\/h2><p>At the time of writing, Laravel ships with 43 individual stub files. A stub file is used each time one of the <code>artisan make<\/code> commands is issued. Since these files are included with your Laravel installation, it makes sense that they reside within our vendors folder by default.<\/p><p>Typically speaking, the vendors folder is where all of your PHP\/Composer dependencies are stored. If you\u2019re using VCS (ex: Git, SVN\u2026 ) for your project files, it\u2019s a good practice to exclude this entire folder. Other developers working on your project are able to download your codebase, and simply run <code>composer install<\/code> to download all of the same dependencies.<\/p><p>With that being said, it means we should never alter these files. Not to worry though, we can safely make a copy, store our copy in an editable folder, and make sure that Laravel\u2019s internals will use <em>our<\/em> version. To do this, simply run the <code>artisan stub:publish<\/code> command.<strong>*<\/strong><\/p><p>Once you\u2019ve issued the publishing command, you\u2019ll notice that you have stub files present within your <code>\/stubs<\/code> folder (which would\u2019ve been created if it didn\u2019t already exist).<\/p><p>These files can be edited safely, and will now be used instead of the ones that were contained inside of your <code>\/vendor<\/code> folder.<\/p><p><em>Upcoming major updates to Laravel may contain changes to the stub files. This means that if you update your version of Laravel after publishing and changing stub files (or any other published files for that matter), you might want to consider republishing, and reincorporating any changes you might have originally made.<\/em><\/p><h2 class=\"wp-block-heading\" id=\"so-many-stubs\" style=\"font-style:normal;font-weight:400\">So Many Stubs<\/h2><p>Many stubs come with Laravel. There are several stubs for controllers, models, and other types of Laravel classes, so figuring out exactly which stub gets invoked by which command can be tricky. I\u2019ve compiled a table that will hopefully help:<\/p><figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th><strong>Stub File<\/strong><\/th><th><strong>Artisan Make Command<\/strong><\/th><\/tr><tr><td>cast.inbound.stub<\/td><td><code>make:cast<\/code><\/td><\/tr><tr><td>cast.stub<\/td><td><code>make:cast<\/code><\/td><\/tr><tr><td>console.stub<\/td><td><code>make:command<\/code><\/td><\/tr><tr><td>controller.api.stub<\/td><td><code>make:controller --api<\/code><\/td><\/tr><tr><td>controller.invokable.stub<\/td><td><code>make:controller --invokable<\/code><\/td><\/tr><tr><td>controller.model.api.stub<\/td><td><code>make:controller --api --model<\/code><\/td><\/tr><tr><td>controller.model.stub<\/td><td><code>make:controller --model<\/code><\/td><\/tr><tr><td>controller.nested.api.stub<\/td><td><code>make:controller --api --parent=Parent<\/code><\/td><\/tr><tr><td>controller.nested.singleton.api.stub<\/td><td><code>make:controller --api --parent=Parent --singleton<\/code><\/td><\/tr><tr><td>controller.nested.singleton.stub<\/td><td><code>make:controller --parent=Parent --singleton<\/code><\/td><\/tr><tr><td>controller.nested.stub<\/td><td><code>make:controller --parent=Parent<\/code><\/td><\/tr><tr><td>controller.plain.stub<\/td><td><code>make:controller<\/code><\/td><\/tr><tr><td>controller.singleton.api.stub<\/td><td><code>make:controller --api --singleton<\/code><\/td><\/tr><tr><td>controller.singleton.stub<\/td><td><code>make:controller --singleton<\/code><\/td><\/tr><tr><td>controller.stub<\/td><td><code>make:controller<\/code><\/td><\/tr><tr><td>event.stub<\/td><td><code>make:event<\/code><\/td><\/tr><tr><td>factory.stub<\/td><td><code>make:factory<\/code><\/td><\/tr><tr><td>job.queued.stub<\/td><td><code>make:job --queued<\/code><\/td><\/tr><tr><td>job.stub<\/td><td><code>make:job<\/code><\/td><\/tr><tr><td>mail.stub<\/td><td><code>make:mail<\/code><\/td><\/tr><tr><td>markdown-mail.stub<\/td><td><code>make:mail --markdown<\/code><\/td><\/tr><tr><td>markdown-notification.stub<\/td><td><code>make:notification --markdown<\/code><\/td><\/tr><tr><td>middleware.stub<\/td><td><code>make:middleware<\/code><\/td><\/tr><tr><td>migration.create.stub<\/td><td><code>make:migration --create<\/code><\/td><\/tr><tr><td>migration.stub<\/td><td><code>make:migration<\/code><\/td><\/tr><tr><td>migration.update.stub<\/td><td><code>make:migration --update<\/code><\/td><\/tr><tr><td>model.pivot.stub<\/td><td><code>make:model --pivot<\/code><\/td><\/tr><tr><td>model.stub<\/td><td><code>make:model<\/code><\/td><\/tr><tr><td>notification.stub<\/td><td><code>make:notification<\/code><\/td><\/tr><tr><td>observer.plain.stub<\/td><td><code>make:observer<\/code><\/td><\/tr><tr><td>observer.stub<\/td><td><code>make:observer --plain<\/code><\/td><\/tr><tr><td>policy.plain.stub<\/td><td><code>make:policy<\/code><\/td><\/tr><tr><td>policy.stub<\/td><td><code>make:policy --plain<\/code><\/td><\/tr><tr><td>provider.stub<\/td><td><code>make:provider<\/code><\/td><\/tr><tr><td>request.stub<\/td><td><code>make:request<\/code><\/td><\/tr><tr><td>resource-collection.stub<\/td><td><code>make:resource --collection<\/code><\/td><\/tr><tr><td>resource.stub<\/td><td><code>make:resource<\/code><\/td><\/tr><tr><td>rule.stub<\/td><td><code>make:rule<\/code><\/td><\/tr><tr><td>scope.stub<\/td><td><code>make:scope<\/code><\/td><\/tr><tr><td>seeder.stub<\/td><td><code>make:seeder<\/code><\/td><\/tr><tr><td>test.stub<\/td><td><code>make:test<\/code><\/td><\/tr><tr><td>test.unit.stub<\/td><td><code>make:test --unit<\/code><\/td><\/tr><tr><td>view-component.stub<\/td><td><code>make:view-component<\/code><\/td><\/tr><\/tbody><\/table><\/figure><h2 class=\"wp-block-heading\" id=\"where-to-next\" style=\"font-style:normal;font-weight:400\">Where To Next?<\/h2><p>You now know what stub files are, how to publish them, and which are used for different purposes, but you might be wondering what to do with this information. While I cannot give you a definitive answer (since one Laravel project can wildly differ from the next), I will give you a few scenarios with stubs to inspire you to make good use of them.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"naming-conventions-basic\">Naming Conventions (Basic)<\/h3><p>Does your organization use different naming conventions from those which Laravel typically uses out-of-the-box? If so, then I recommend <a href=\"https:\/\/laravel.com\/docs\/10.x\/artisan#stub-customization\" target=\"_blank\" rel=\"noreferrer noopener\">updating your stubs<\/a> with your naming conventions. Any time your developers create new files, they will start off with your proper naming conventions in place. If a file is already using one particular naming convention, then your developers are more likely to continue to follow suit.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"simple-modifications-basic\">Simple Modifications (Basic)<\/h3><p>I\u2019ve worked on a number of Laravel projects that used the SoftDeletes trait in pretty much every model. It\u2019s really easy to add soft deletes to your models, but it\u2019s also something that\u2019s really easy to forget about. Are you starting a new project where you will require records to be soft deleted in every table? If so, then I would recommend updating your <code>model.stub<\/code> to include the boilerplate code for soft deleting records. Once you\u2019ve done that, all subsequent models created via <code>artisan make:model<\/code> will be already equipped for soft deletion.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"new-workflows-intermediate\">New Workflows (Intermediate)<\/h3><p>Some systems we\u2019ve worked on require a connection to an external API, and in some of those cases, we end up creating several files\u2014each of which interacts with a different part of the API. Oftentimes, the separate files we create will have commonalities; when we do that, we end up creating our own artisan command, which, when run, will utilize our own custom stub. Doing this can be a huge time-saver when collaborating with multiple developers. It\u2019s usually best that whomever knows the workflow and external API is the one creating the new stub and command. That way, other developers who don\u2019t yet understand the external API or workflow can get up and running with boilerplate code in a familiar Laravel way in no time.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"overriding-artisan-make-advanced\">Overriding Artisan Make (Advanced)<\/h3><p>It\u2019s even possible to override what happens when an <code>artisan make<\/code> command is issued. Discussing how to do so is beyond the scope of this article, but I want to provide an example of how this can be used. It\u2019s pretty common for us to use <code>artisan make:model -a<\/code>. Doing so will create a model, resourceful controller, policy, a couple of request files, a seeder, a factory, and a database migration script. We\u2019ve expanded the command to also create a couple of Vue files for us. One represents a list view for the new model, and one, a details view. Having done this, when we create a new module for a project we\u2019re working on, we can have a working (albeit simple) list and details view within a couple of minutes\u2014pretty game changing!<\/p><h2 class=\"wp-block-heading\" id=\"wrapping-up\" style=\"font-style:normal;font-weight:400\">Wrapping Up<\/h2><p>I hope you have a better understanding of what Stubs are, and what kinds of QOL improvements they can make for you and your team. Let us know how you\u2019re using them in your project, and what kind of impact they\u2019ve had for your organization.<\/p><h6 class=\"wp-block-heading\" id=\"artisan-is-a-laravel-command-that-needs-to-be-executed-through-php-these-commands-can-be-run-using-php-artisan-if-being-used-locally-or-vendor-bin-sail-artisan-if-youre-using-laravel-sail\"><strong>*<\/strong>`artisan` is a Laravel command that needs to be executed through PHP. These commands can be run using <code>php artisan<\/code> if being used locally, or <code>.\/vendor\/bin\/sail artisan<\/code> if you\u2019re using Laravel Sail.<\/h6>","protected":false},"excerpt":{"rendered":"<p>Stubs are class templates that come with Laravel. They get used behind the scenes whenever you create new files within your project. Stubs are hidden from view by default, so I don\u2019t think they are appreciated enough. Chances are, if you have worked with Laravel, you\u2019ve used an artisan make command to make a new &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.directimpactsolutions.com\/en\/laravel-stubs\/\"> <span class=\"screen-reader-text\">Laravel Stubs<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":7,"featured_media":16705,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","footnotes":""},"categories":[32],"tags":[],"class_list":["post-16698","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-applications"],"uagb_featured_image_src":{"full":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS.png",500,300,false],"thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS-150x150.png",150,150,true],"medium":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS-300x180.png",300,180,true],"medium_large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS.png",500,300,false],"large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS.png",500,300,false],"1536x1536":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS.png",500,300,false],"2048x2048":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS.png",500,300,false],"woocommerce_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS-300x300.png",300,300,true],"woocommerce_single":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS.png",500,300,false],"woocommerce_gallery_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/02\/LARAVEL-STUBS-100x100.png",100,100,true]},"uagb_author_info":{"display_name":"Matt Leering","author_link":"https:\/\/www.directimpactsolutions.com\/en\/author\/matt-leering\/"},"uagb_comment_info":0,"uagb_excerpt":"Stubs are class templates that come with Laravel. They get used behind the scenes whenever you create new files within your project. Stubs are hidden from view by default, so I don\u2019t think they are appreciated enough. Chances are, if you have worked with Laravel, you\u2019ve used an artisan make command to make a new&hellip;","_links":{"self":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/16698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/comments?post=16698"}],"version-history":[{"count":4,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/16698\/revisions"}],"predecessor-version":[{"id":19971,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/16698\/revisions\/19971"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media\/16705"}],"wp:attachment":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media?parent=16698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/categories?post=16698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/tags?post=16698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}