<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: David Carr</title>
    <description>The latest articles on Forem by David Carr (@dcblog).</description>
    <link>https://forem.com/dcblog</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F96028%2F06b208b7-6175-4d66-80f3-04672fbdc8cd.jpg</url>
      <title>Forem: David Carr</title>
      <link>https://forem.com/dcblog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dcblog"/>
    <language>en</language>
    <item>
      <title>15 years of blogging 500 posts later</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Thu, 02 May 2024 11:37:01 +0000</pubDate>
      <link>https://forem.com/dcblog/15-years-of-blogging-500-posts-later-4bn4</link>
      <guid>https://forem.com/dcblog/15-years-of-blogging-500-posts-later-4bn4</guid>
      <description>&lt;p&gt;I started blogging in 2009 mainly documenting my learning while at University. Most of my early posts were very basic but proved useful to look back on when I had forgotten how to do something.&lt;/p&gt;

&lt;p&gt;My classmates also found it useful to follow along on simple things like how to connect a PHP script to a MySQL database for example. Over the years my tutorials got more involved as I learned more.&lt;/p&gt;

&lt;p&gt;I've managed to keep writing for the last 15 years, which is a massive achievement. I've seen many dev blogs come and go over the years. Writing regularly can be challenging, but I've found the best way to handle this is not to put myself under pressure and write only when I have something in mind, rather than forcing posts out.&lt;/p&gt;

&lt;p&gt;Most of my posts document problem-solving.&lt;/p&gt;

&lt;h2&gt;
  
  
  Increase Knowledge
&lt;/h2&gt;

&lt;p&gt;Writing blog posts significantly contributes to increasing knowledge. As you delve into various topics and subjects to write about, you inevitably learn more about them. This process involves researching, understanding, and translating complex information into a more digestible format. It's like teaching you learn more when you teach because you need to understand the subject matter deeply enough to explain it to others.&lt;/p&gt;

&lt;p&gt;Moreover, blogging prompts you to stay updated with the latest trends and developments in your field. To produce relevant and timely content, you need to be aware of what's happening in your industry. This constant learning and staying abreast of new information expands your knowledge base.&lt;/p&gt;

&lt;h2&gt;
  
  
  Helping People
&lt;/h2&gt;

&lt;p&gt;I enjoy helping others, and it's gratifying that over the years, my blog has assisted numerous developers worldwide. It has enabled me to connect with others and learn from them.&lt;/p&gt;

&lt;p&gt;Many of my early posts were a direct result of participating in Facebook groups and answering PHP questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Helping to secure jobs
&lt;/h2&gt;

&lt;p&gt;Having an active blog and engaging in open-source work has aided me in my job search. Interviewers have directly mentioned my packages and writing, stating that they already know me and have been following my work. Putting yourself out there can be intimidating, but it's well worth it.&lt;/p&gt;

&lt;p&gt;In conclusion, blogging over the last 15 years has been a rewarding journey. It has not only helped to increase my knowledge base, but it has also provided a platform to assist others. The process of writing and sharing my experiences and learnings has been instrumental in connecting with others and staying updated with industry trends. Moreover, maintaining an active blog and participating in open-source work has significantly contributed to my professional growth. Despite the challenges, the benefits of blogging are well worth the effort. I look forward to continuing this journey, learning more, and sharing more in the years to come.&lt;/p&gt;

</description>
      <category>blogging</category>
    </item>
    <item>
      <title>Customize Your Hashnode Blog Frontend with Headless Frontend and Laravel</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Fri, 12 Apr 2024 11:26:00 +0000</pubDate>
      <link>https://forem.com/dcblog/composer-scripts-boosting-your-teams-efficiency-1fcp</link>
      <guid>https://forem.com/dcblog/composer-scripts-boosting-your-teams-efficiency-1fcp</guid>
      <description>&lt;p&gt;I've built a new site called &lt;a href="https://headlessfrontend.dev/"&gt;headlessfrontend.dev&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first focus is using &lt;a href="https://hashnode.com/headless"&gt;Hashnode's API&lt;/a&gt; to pull in your posts and pages into a Laravel application when using headless within your blog advanced settings page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RCrcqX08--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1716806570193/74c567f0-6bc9-4cdf-94b2-c99f6ccde5a0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RCrcqX08--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1716806570193/74c567f0-6bc9-4cdf-94b2-c99f6ccde5a0.png" alt="Hashnode Settings" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm using Headless mode with Hashnode on this blog, I'm a huge fan of this approach.&lt;/p&gt;

&lt;p&gt;This allows me to still be part of the Hahsnode community and have my posts shared into the Hashnode feeds whilst being in full control over my frontend.&lt;/p&gt;

&lt;p&gt;I really love Hashnode's editor and AI integration so this allows me to have the best of both worlds.&lt;/p&gt;

&lt;p&gt;I can now have my blog as part of my site and not restricted to choose between a sub domain or a main domain with just hashnode content. I can mix and match. For instance my &lt;a href="https://dcblog.dev/docs"&gt;Packages&lt;/a&gt;, &lt;a href="https://dcblog.dev/books"&gt;Books&lt;/a&gt; and &lt;a href="https://dcblog.dev/contact"&gt;Contact&lt;/a&gt; pages are all custom Laravel pages.&lt;/p&gt;

&lt;p&gt;With &lt;a href="https://headlessfrontend.dev/"&gt;headlessfrontend.dev&lt;/a&gt; pick a theme from the Hashnode category then pull in your blog posts by adding your hashnode URL into the &lt;code&gt;.env&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="no"&gt;HASHNODE_HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;dcblog&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The laravel applications use Hashnode's GraphQL API to pull in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Posts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Series&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pages&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Followers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nav Menu including their items for posts, series and pages&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;RSS feed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Favicon&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Social icons (GitHub, Twitter, Facebook etc)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SEO meta tags&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Team Logo (for teams) or Author Logo (for single publications)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;About text&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Newsletter integration&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Search posts integration&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This gives you full control over the design and layout.&lt;/p&gt;

&lt;p&gt;Checkout &lt;a href="http://headlessfrontend.dev"&gt;headlessfrontend.dev&lt;/a&gt; for all available templates.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>hashnode</category>
      <category>headless</category>
      <category>templates</category>
    </item>
    <item>
      <title>GitHub patch release previous version</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Wed, 07 Feb 2024 15:37:00 +0000</pubDate>
      <link>https://forem.com/dcblog/github-patch-release-previous-version-5dok</link>
      <guid>https://forem.com/dcblog/github-patch-release-previous-version-5dok</guid>
      <description>&lt;p&gt;On Github you way from time to time need to patch release the previous version of a published release.&lt;/p&gt;

&lt;p&gt;This took me longer than I care to admit to understand the steps involved. Hence I'm documenting them in this post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Check out the branch corresponding to the previous version of your code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout 7.3.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Fix the bug in the code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's say it's a breaking change that's been fixed in the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Commit the changes with a version number that indicates the patch release.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Commit the fix&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git commit -m "Fix bug in 7.3.0"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, create a new tag&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git tag 7.4.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Push the changes to the remote repository.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git push origin 7.4.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Create a new release for the patch version on GitHub.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Navigate to the main page of the repository on GitHub.com, click on the Releases tab, and then click on the "Draft a new release" button.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Fill in the details for the release in the form, select the tag created from Step 3, and fill in the release notes.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once you have filled in the details, click on the "Publish release" button to publish the release.&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
    </item>
    <item>
      <title>How to disable Laravel console commands</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Tue, 06 Feb 2024 11:37:46 +0000</pubDate>
      <link>https://forem.com/dcblog/how-to-disable-laravel-console-commands-5d8g</link>
      <guid>https://forem.com/dcblog/how-to-disable-laravel-console-commands-5d8g</guid>
      <description>&lt;p&gt;Suppose you find yourself in a situation where you need to disable or intercept a console command in Laravel. This tutorial will primarily focus on how to intercept the &lt;code&gt;php artisan migrate&lt;/code&gt; command. We'll delve into the command method &lt;code&gt;app/Console/Kernel.php&lt;/code&gt; and explore how to prevent the migrate command from executing any actions.&lt;/p&gt;

&lt;p&gt;Inside the command method of &lt;code&gt;app/Console/Kernel.php&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Laravel 11 use routes/console.php&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Intercept command
&lt;/h2&gt;

&lt;p&gt;You can intercept &lt;code&gt;php artisan migrate&lt;/code&gt; and, instead, catch it and use a closure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Artisan::command('migrate', function () { //});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would essentially stop the migrate command from doing anything.&lt;/p&gt;

&lt;p&gt;You could then print a message out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Artisan::command('migrate', function () { $this-&amp;gt;info('Command NOT AVAILABLE. Use this: php artisan app:migrate');});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Running an alternative command:
&lt;/h2&gt;

&lt;p&gt;To run a different command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Artisan::command('migrate', function () { $this-&amp;gt;call('app:migrate');});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, a new command called &lt;code&gt;app:migrate&lt;/code&gt; would be executed.&lt;/p&gt;

&lt;p&gt;Run command with a --path option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Artisan::command('migrate', function () { $this-&amp;gt;call('app:migrate', ['--path' =&amp;gt; 'database/migrations']);});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when running &lt;code&gt;php artisan migrate&lt;/code&gt; you will get an error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;The "--path" option does not exist.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The reason for this is the custom command does not have a path option. Lets create the command and add the option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a command:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:command AppMigrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a class called &lt;code&gt;AppMigrate.php&lt;/code&gt; inside &lt;code&gt;app/Console/Commands&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Change the signature to be &lt;code&gt;app:migrate&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?phpnamespace App\Console\Commands;use Illuminate\Console\Command;class AppMigrate extends Command{ / *** The name and signature of the console command. * * @var string */ protected $signature = 'app:migrate'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Execute the console command. */ public function handle() { // }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now for this command to accept a path change the signature to accept a path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected $signature = 'app:migrate {--path= : The path to the migrations}';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then inside the &lt;code&gt;handle&lt;/code&gt; method collect the path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle(){ $path = $this-&amp;gt;option('path') ?? 'database/other-path/migrations'; //rest of the logic ...}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>console</category>
      <category>commands</category>
    </item>
    <item>
      <title>The podcasts I listen to as a PHP developer</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Fri, 19 Jan 2024 15:32:24 +0000</pubDate>
      <link>https://forem.com/dcblog/the-podcasts-i-listen-to-as-a-php-developer-3m70</link>
      <guid>https://forem.com/dcblog/the-podcasts-i-listen-to-as-a-php-developer-3m70</guid>
      <description>&lt;p&gt;The following are the podcasts I listen to. In no particular order. (That have episodes recently)&lt;/p&gt;

&lt;p&gt;Listen to a podcast not listed? Please do comment and let me know.&lt;/p&gt;

&lt;h1&gt;
  
  
  APIs You Won't Hate (The Podcast)
&lt;/h1&gt;

&lt;p&gt;"APIs You Won't Hate founders Phil Sturgeon and Mike Bifulco chat with devs and product builders from the API Developer Tools industry. Learn about news with API Tools, SDK Code generation, OpenAPI, API Documentation, Swagger, GraphQL, REST APIs, and gRPC."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://apisyouwonthate.com/tag/podcast/"&gt;https://apisyouwonthate.com/tag/podcast&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Build your SAAS
&lt;/h2&gt;

&lt;p&gt;"Interested in building your own SaaS company? Follow the journey of &lt;a href="https://transistor.fm/?via=podcast"&gt;&lt;strong&gt;Transistor.fm&lt;/strong&gt;&lt;/a&gt; as they bootstrap a podcast hosting startup."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://saas.transistor.fm/"&gt;https://saas.transistor.fm&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Call it a Day
&lt;/h2&gt;

&lt;p&gt;"The episodes will be about my coding and business life. This could be about PHP, Laravel, business decisions, speaking at conferences, my products and more."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://callitaday.transistor.fm/"&gt;https://callitaday.transistor.fm&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Framework Friends
&lt;/h2&gt;

&lt;p&gt;"Aaron Francis and Andrew Culver compare notes on their experiences as developers in the Laravel and Ruby on Rails ecosystems."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.frameworkfriends.com/"&gt;https://www.frameworkfriends.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Frontmatter
&lt;/h2&gt;

&lt;p&gt;"In Leanpub's Frontmatter podcast, we interview authors and special guests about their lives &amp;amp; careers, their areas of expertise and the issues of the moment, and their experiences as writers. Every episode is deeply researched and covers areas that are equally of human interest, general interest, and professional interest."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://leanpub.com/podcasts/frontmatter"&gt;https://leanpub.com/podcasts/frontmatter&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Grumpy Old Geeks
&lt;/h2&gt;

&lt;p&gt;"Hosts Jason DeFillippo &amp;amp; Brian Schulmeister have over 40 years of professional website building experience between them. Every week they discuss what went wrong on the Internet and who's to blame."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.gog.show/"&gt;https://www.gog.show&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Code Well
&lt;/h2&gt;

&lt;p&gt;"Web development discussions and interviews from Peter Fisher. Covering JavaScript, Linux, Docker, PHP, Python, MYSQL, HTML and more"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://open.spotify.com/show/6zapPUKAuSjEZxrKR5fUxW"&gt;https://open.spotify.com/show/6zapPUKAuSjEZxrKR5fUxW&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Laravel News Podcast
&lt;/h2&gt;

&lt;p&gt;"The Laravel News podcast, with hosts &lt;a href="https://podcast.laravel-news.com/people/jacob-bennett"&gt;&lt;strong&gt;Jacob Bennett&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://podcast.laravel-news.com/people/michael-dyrynda"&gt;&lt;strong&gt;Michael Dyrynda&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;,&lt;/strong&gt; covers all the latest Laravel news, Packages, Events, and everything else new in the community. "&lt;/p&gt;

&lt;p&gt;&lt;a href="https://podcast.laravel-news.com/"&gt;https://podcast.laravel-news.com/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Make Lemonade
&lt;/h2&gt;

&lt;p&gt;"The show features various entrepreneurs, creators, business executives, and more all sharing their successes, failures, and everything else in between."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lemonsqueezy.com/blog/make-lemonade-podcast#:~:text=What%20to%20expect%20from%20the,catching%20up%20on%20the%20show"&gt;https://www.lemonsqueezy.com/blog/make-lemonade-podcast&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mostly Technical
&lt;/h2&gt;

&lt;p&gt;"Hosted by Ian Landsman and Aaron Francis, Mostly Technical is a lively discussion on Laravel, business, and an eclectic mix of related topics."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://mostlytechnical.com/"&gt;https://mostlytechnical.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  North meets South
&lt;/h2&gt;

&lt;p&gt;"Jacob Bennett and Michael Dyrynda conquer a 14.5-hour time difference to talk about life as web developers"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.northmeetssouth.audio/"&gt;https://www.northmeetssouth.audio&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Over Engineered
&lt;/h2&gt;

&lt;p&gt;"A podcast where we explore unimportant programming questions (mostly PHP/Laravel/JavaScript) in extreme detail."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://overengineered.fm/"&gt;https://overengineered.fm&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Software Unscripted
&lt;/h2&gt;

&lt;p&gt;"Software Unscripted, A weekly podcast of casual conversations about code hosted by Richard Feldman &amp;amp; sponsored by NoRedInk."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://feeds.resonaterecordings.com/software-unscripted"&gt;https://feeds.resonaterecordings.com/software-unscripted&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;

&lt;p&gt;"&lt;em&gt;A Tasty Treats Podcast for Web Developers&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://syntax.fm/"&gt;https://syntax.fm&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Changelog
&lt;/h2&gt;

&lt;p&gt;"Software's best weekly news brief, deep technical interviews &amp;amp; talk show"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://changelog.com/podcast"&gt;https://changelog.com/podcast&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Laracasts Snippet
&lt;/h2&gt;

&lt;p&gt;"The Laracasts snippet, each episode, offers a single thought on some aspect of web development."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://laracasts.com/podcast"&gt;https://laracasts.com/podcast&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Laravel Podcast
&lt;/h2&gt;

&lt;p&gt;"The Laravel Podcast brings you Laravel and PHP development news and discussion."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://laravelpodcast.com/"&gt;https://laravelpodcast.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Listen to a podcast not listed? Please do comment and let me know.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating Flexible Layouts in Laravel with Yields, Includes, and Slots</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Thu, 18 Jan 2024 09:29:46 +0000</pubDate>
      <link>https://forem.com/dcblog/creating-flexible-layouts-in-laravel-with-yields-includes-and-slots-2j9</link>
      <guid>https://forem.com/dcblog/creating-flexible-layouts-in-laravel-with-yields-includes-and-slots-2j9</guid>
      <description>&lt;h2&gt;
  
  
  Yields
&lt;/h2&gt;

&lt;p&gt;In Laravel, the &lt;code&gt;@yield&lt;/code&gt; directive is used in &lt;a href="https://laravel.com/docs/10.x/blade"&gt;blade templates&lt;/a&gt; to define a section that can have code injected or "yielded" by child views.&lt;/p&gt;

&lt;p&gt;Here's a basic explanation of how &lt;code&gt;@yield&lt;/code&gt; works:&lt;/p&gt;

&lt;p&gt;Defining a section with &lt;code&gt;@yield('name')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In a blade file typically used for layouts (can be any *.blade.php file), you can use the &lt;code&gt;@yield&lt;/code&gt; directive to define a section where content can be injected. For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;layouts/app.blade.php&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;@yield('title') - {{ conifig('app.name') }}&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        @yield('content')
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, there are two &lt;code&gt;@yield&lt;/code&gt; directives: one for the 'title' section and another for the 'content' section.&lt;/p&gt;

&lt;p&gt;In a child view that extends the parent view, you can use the &lt;code&gt;@section&lt;/code&gt; directive to fill the sections defined in the parent view.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;extends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'layouts.app'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nf"&gt;section&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Page Title'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nf"&gt;section&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'content'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;This&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;endsection&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the view extends from &lt;code&gt;layouts/app.blade.php&lt;/code&gt; the &lt;code&gt;blade.php&lt;/code&gt; is left off the path as Laravel will know the file by its name only.&lt;/p&gt;

&lt;p&gt;Then fill the 'title' and 'content' sections using a &lt;code&gt;@section&lt;/code&gt; directive. This directive can self-close like in the title example or enclose multiple lines like in the ‘content’ example.&lt;/p&gt;

&lt;p&gt;When you render the child view, Laravel will combine the content of the child view with the layout defined in the parent view. The &lt;code&gt;@yield&lt;/code&gt; directives in the parent view will be replaced with the content provided in the child view. php  The resulting HTML will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Page Title&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is the content of the page.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Includes
&lt;/h2&gt;

&lt;p&gt;For more flexibility, you can break your views into smaller chunks using multiple views, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'layouts.partials.header'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"wrapper"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'content'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'layouts.partials.footer'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This layout view uses both &lt;code&gt;@include&lt;/code&gt; and &lt;code&gt;@yield&lt;/code&gt; directives. An include is used to bring in other files into the current file where the &lt;code&gt;@include&lt;/code&gt; is placed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@include&lt;/code&gt; takes 2 arguments. A file path and optionally an array. By default, anything defined in the parent page is available to the included file but it’s better to be explicit by passing in an array.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pages'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'items'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$arrayOfItems&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example would include a &lt;code&gt;pages.blade.php&lt;/code&gt; file and pass in an array called items. Inside the view &lt;code&gt;$pages&lt;/code&gt; could then be used.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@include('layouts.partials.header')&lt;/code&gt; would include a file called &lt;code&gt;header.blade.php&lt;/code&gt; located in &lt;code&gt;layouts/partials&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Typically I define multiple &lt;code&gt;yields&lt;/code&gt; in a header like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    @yield('meta')
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;@yield('title') - {{ config('app.name') }}&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    @yield('css')
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
@yield('js')
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows me to inject code into various parts of my layout.&lt;/p&gt;

&lt;p&gt;For example in a blog I want to use meta tags for that post only and no other pages. This means I cannot hardcode the meta tags and the contents should change from post to post.&lt;/p&gt;

&lt;p&gt;Typically I would inject meta tags in a post view like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nf"&gt;section&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'meta'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;itemprop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ &lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;title&lt;/span&gt;&lt;span class="s2"&gt; }}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;itemprop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"description"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{!! strip_tags(Str::limit(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;description&lt;/span&gt;&lt;span class="s2"&gt;, 100)) !!}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;itemprop&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"image"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ url(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;image&lt;/span&gt;&lt;span class="s2"&gt;) }}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;endif&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'description'&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'{!! strip_tags(Str::limit($post-&amp;gt;description, 100)) !!}'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"article:published_time"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ &lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;created_at&lt;/span&gt;&lt;span class="s2"&gt; }}"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"article:modified_time"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ &lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;updated_at&lt;/span&gt;&lt;span class="s2"&gt; }}"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;!--&lt;/span&gt; &lt;span class="nc"&gt;Open&lt;/span&gt; &lt;span class="nc"&gt;Graph&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nc"&gt;Facebook&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"og:type"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"website"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"og:url"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ url(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;slug&lt;/span&gt;&lt;span class="s2"&gt;) }}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"og:title"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ &lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;title&lt;/span&gt;&lt;span class="s2"&gt; }}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"og:description"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{!! strip_tags(Str::limit(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;description&lt;/span&gt;&lt;span class="s2"&gt;, 100)) !!}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"og:image"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ url(trim(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;image&lt;/span&gt;&lt;span class="s2"&gt;)) }}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;endif&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;!--&lt;/span&gt; &lt;span class="nc"&gt;Twitter&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"twitter:card"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"summary_large_image"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"twitter:site"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"@dcblogdev"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"twitter:creator"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"@dcblogdev"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"twitter:url"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ url(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;slug&lt;/span&gt;&lt;span class="s2"&gt;) }}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"twitter:title"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ &lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;title&lt;/span&gt;&lt;span class="s2"&gt; }}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"twitter:description"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{!! strip_tags(Str::limit(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;description&lt;/span&gt;&lt;span class="s2"&gt;, 100)) !!}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;meta&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"twitter:image"&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{{ url(trim(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;image&lt;/span&gt;&lt;span class="s2"&gt;)) }}"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;endif&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="n"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"canonical"&lt;/span&gt; &lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'{{ url($post-&amp;gt;slug) }}'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="n"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"webmention"&lt;/span&gt; &lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'https://webmention.io/dcblog.dev/webmention'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="n"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"pingback"&lt;/span&gt; &lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://webmention.io/dcblog.dev/xmlrpc"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="n"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"pingback"&lt;/span&gt; &lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://webmention.io/webmention?forward={{ url(&lt;/span&gt;&lt;span class="nv"&gt;$post-&amp;gt;slug&lt;/span&gt;&lt;span class="s2"&gt;) }}"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;endSection&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I may also inject CSS or Javascript from a single view, this is done in the same way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nf"&gt;section&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'js'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;endSection&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Slots
&lt;/h2&gt;

&lt;p&gt;When working with components or Livewire you will come across the concept of slots.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a slot?
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;$slot&lt;/code&gt; variable is a special variable used to reference the content that is passed into a component. Components are a way to create reusable and encapsulated pieces of view logic.&lt;/p&gt;

&lt;p&gt;Here's a brief explanation of how &lt;code&gt;$slot&lt;/code&gt; works within Laravel components:&lt;/p&gt;

&lt;h3&gt;
  
  
  Components
&lt;/h3&gt;

&lt;p&gt;When you create a Blade component, you can define a slot within it using the &lt;code&gt;{{ $slot }}&lt;/code&gt; syntax.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- resources/views/components/alert.blade.php --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"alert"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ $slot }}
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the alert component has a slot where content can be injected.&lt;/p&gt;

&lt;p&gt;When you use the component in another view, you can pass content into the slot using the component tag.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- resources/views/welcome.blade.php --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;x-alert&amp;gt;&lt;/span&gt;
    This is the content for the alert.
&lt;span class="nt"&gt;&amp;lt;/x-alert&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the content "This is the content for the alert." is passed into the &lt;code&gt;$slot&lt;/code&gt; of the alert component.&lt;/p&gt;

&lt;p&gt;When the Blade view is rendered, Laravel will replace the &lt;code&gt;{{ $slot }}&lt;/code&gt; in the component with the content provided when using the component. The resulting HTML will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"alert"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    This is the content for the alert.
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;$slot&lt;/code&gt; variable essentially acts as a placeholder for the content passed to the component.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;$slot&lt;/code&gt; allows you to create flexible and reusable components that can accept different content each time they are used. It provides a convenient way to structure and organize your Blade templates while maintaining the reusability of components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Component Layouts
&lt;/h2&gt;

&lt;p&gt;Components can also be layout files, for example making a new component called &lt;code&gt;AppLayout&lt;/code&gt; using Artisan:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="n"&gt;php&lt;/span&gt; &lt;span class="n"&gt;artisan&lt;/span&gt; &lt;span class="n"&gt;make&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;component&lt;/span&gt; &lt;span class="nc"&gt;AppLayout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will create 2 files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;app/View/Components/AppLayout.php&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;resources/views/components/app-layout.blade.php&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Replace the contents of &lt;code&gt;AppLayout.php&lt;/code&gt; with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\View\Components&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\View\Component&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\View\View&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppLayout&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * Get the view / contents that represents the component.
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'layouts.app'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will load a &lt;code&gt;layouts/app.blade.php&lt;/code&gt; when rendered.&lt;/p&gt;

&lt;p&gt;To have a view use this layout inside a view use the &lt;code&gt;&amp;lt;x-&lt;/code&gt; directive followed by the component name in the kebab case. So &lt;code&gt;AppLayout&lt;/code&gt; becomes &lt;code&gt;app-layout&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;x-app-layout&amp;gt;&lt;/span&gt;
    The content goes here.
&lt;span class="nt"&gt;&amp;lt;/x-app-layout&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now inside &lt;code&gt;app.blade.php&lt;/code&gt; since this used the &lt;code&gt;AppLayout&lt;/code&gt; component we don’t use &lt;code&gt;@yield&lt;/code&gt; to have placeholders instead, we use &lt;code&gt;{{ $slot }}&lt;/code&gt; or named slots&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"{{ str_replace('_', '-', app()-&amp;gt;getLocale()) }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;{{ $title ?? null }} - {{ config('app.name', 'Laravel') }}&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        {{ $slot }}
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What are named slots?
&lt;/h3&gt;

&lt;p&gt;In components, named slots provide a way to define and pass content into specific sections of a component. Unlike the default slot, which is referenced using, named slots allow you to define multiple distinct sections for injecting content. This can be particularly useful when you need to organize and structure different parts of your component.&lt;/p&gt;

&lt;p&gt;Here's a basic explanation of how named slots work.&lt;/p&gt;

&lt;p&gt;When creating a blade component, you can define named slots using the &lt;code&gt;@slot&lt;/code&gt; directive. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- resources/views/components/alert.blade.php --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"alert"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        @slot('header')
            {{ $header ?? '' }}
        @endslot
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        {{ $slot }}
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        @slot('footer')
            {{ $footer ?? '' }}
        @endslot
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the alert component has three named slots: 'header', 'footer', and the default slot, which is simply referred to as &lt;code&gt;$slot&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When using the component in another blade view, you can pass content into the named slots using the component tag. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- resources/views/welcome.blade.php --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;x-alert&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;x-slot&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Alert Header&lt;span class="nt"&gt;&amp;lt;/x-slot&amp;gt;&lt;/span&gt;
    This is the content for the alert.
    &lt;span class="nt"&gt;&amp;lt;x-slot&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Alert Footer&lt;span class="nt"&gt;&amp;lt;/x-slot&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/x-alert&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, content is provided for the 'header' and 'footer' named slots, in addition to the default slot.&lt;/p&gt;

&lt;p&gt;When the blade view is rendered, Laravel will replace the content of the named slots in the component with the content provided when using the component. The resulting HTML will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"alert"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Alert Header&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;This is the content for the alert.&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Alert Footer&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Named slots provide a way to structure and organize the content of your components more explicitly. They make it easier to manage complex components with multiple distinct sections by giving each section a meaningful name.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Enhancing Laravel Applications with Traits: A Step-by-Step Guide</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Wed, 17 Jan 2024 08:17:33 +0000</pubDate>
      <link>https://forem.com/dcblog/enhancing-laravel-applications-with-traits-a-step-by-step-guide-54n6</link>
      <guid>https://forem.com/dcblog/enhancing-laravel-applications-with-traits-a-step-by-step-guide-54n6</guid>
      <description>&lt;h2&gt;
  
  
  What are traits?
&lt;/h2&gt;

&lt;p&gt;Traits are a mechanism for code reuse. Traits allow you to create methods that can be used in multiple classes to avoid code duplication. They are intended to enhance traditional inheritance, enabling you to reuse sets of methods freely in several independent classes living in different class hierarchies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Trait
&lt;/h2&gt;

&lt;p&gt;Traits are classes nothing more, typically you would create them in a &lt;code&gt;traits&lt;/code&gt; folder or &lt;code&gt;concerns&lt;/code&gt; which has become quite popular in Laravel. I use a traits folder.&lt;/p&gt;

&lt;p&gt;When the traits for written to be used in models I place the traits folder inside a &lt;code&gt;app/Models&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;The skeleton for a trait is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Models\Traits&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;HasUuid&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;booted&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like any other class, a trait needs a namespace so it can be loaded.&lt;/p&gt;

&lt;p&gt;Next name the trait in this example the trait is called HasUuid.&lt;/p&gt;

&lt;p&gt;Inside the traits are either public methods that can be called or like in this case a named function that would be loaded automatically by Laravel using a special &lt;code&gt;booted&lt;/code&gt; name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;To use the above trait in a model. The trait will be imported by calling its namespace &lt;code&gt;use App\Models\Traits\HasUuid;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then inside the class &lt;code&gt;use&lt;/code&gt; followed by the trait name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Models&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Traits\HasUuid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Setting&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;HasUuid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="mf"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A complete UUID trait
&lt;/h2&gt;

&lt;p&gt;In the above examples I’ve mentioned using a trait called &lt;code&gt;HasUuid&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The complete class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Models\Traits&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;HasUuid&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getIncrementing&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getKeyType&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;booted&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;creating&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Set attribute for new model's primary key (ID) to an uuid.&lt;/span&gt;
            &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getKeyName&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;Str&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This trait contains 3 methods. In this case, they are all used internally by Laravel.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getIncrementing&lt;/code&gt; is used to turn off auto-incrementing ids in a database for a primary column.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getKeyType&lt;/code&gt; is used to override the &lt;code&gt;getKeyType&lt;/code&gt; method, which is defined in the parent classes/interfaces. In Laravel, the &lt;code&gt;getKeyType&lt;/code&gt; method is used to specify the data type of the primary key in the model associated with the trait.&lt;/p&gt;

&lt;p&gt;In this specific example, the getKeyType method is hard coded to always return the string 'string'. This means that any model using this trait will have its primary key treated as a string type. For instance, if you have a model that uses this trait, and you retrieve its primary key type using &lt;code&gt;$model-&amp;gt;getKeyType()&lt;/code&gt;. it will always return &lt;code&gt;string&lt;/code&gt; regardless of the actual data type of the primary key column in the database.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;booted&lt;/code&gt; will be called automatically by Laravel, since the method will be called statically it must be defined as a static method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a custom trait.
&lt;/h2&gt;

&lt;p&gt;In Laravel you can’t boot multiple traits so instead it's better to name a trait &lt;code&gt;boot&lt;/code&gt; method in the format of &lt;code&gt;bootHasName&lt;/code&gt; so a trait called &lt;code&gt;HasRoles&lt;/code&gt; would be called &lt;code&gt;bootHasRoles&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This allows booting multiple traits and will still be called automatically when they are used inside other classes.&lt;/p&gt;

&lt;p&gt;A common use case is building a multi-tenancy system. Where when a user is logged in then grab a column from the table such as &lt;code&gt;tenant_id&lt;/code&gt; and then populate any model with the value of the current tenant.&lt;/p&gt;

&lt;p&gt;This avoids having to define a tenend_id any time a model has a create event, this trait will automatically see the create, create, and populate a field called &lt;code&gt;tenant_id&lt;/code&gt; and then apply a global function so only records matching this tenant will be used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Models\Traits&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Builder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;HasTenant&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;bootHasTenant&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$tenantId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;creating&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$tenantId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$tenantId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;

            &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;addGlobalScope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'tenant'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Builder&lt;/span&gt; &lt;span class="nv"&gt;$builder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$tenantId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nv"&gt;$builder&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'tenant_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$tenantId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use both these examples in a single model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Models&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Traits\HasTenant&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Traits\HasUuid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Setting&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;HasTenant&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;HasUuid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;//rest of the model code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Traits in Laravel offer a powerful way to reuse code across different classes, thereby reducing code duplication. By utilizing concepts such as the &lt;code&gt;HasUuid&lt;/code&gt; and &lt;code&gt;HasRoles&lt;/code&gt; traits, Laravel applications can be made more efficient and maintainable.&lt;/p&gt;

&lt;p&gt;Furthermore, Laravel's unique booting mechanism allows for the automatic execution of trait methods, simplifying the implementation of complex functionalities like multi-tenancy. Therefore, understanding and effectively using traits is an essential skill for any Laravel developer.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Laravel ApiResouce name nested route placeholder</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Thu, 11 Jan 2024 00:26:22 +0000</pubDate>
      <link>https://forem.com/dcblog/laravel-apiresouce-name-nested-route-placeholder-33hn</link>
      <guid>https://forem.com/dcblog/laravel-apiresouce-name-nested-route-placeholder-33hn</guid>
      <description>&lt;p&gt;When using an ApiResource that has more than one placeholder it may seem tricky to figure out how to name the placeholder used.&lt;/p&gt;

&lt;p&gt;For example, take this route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::apiResource('users.tenant', UsersController::class);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This produces these routes&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xgiEHjSc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1704931770863/704b5217-11f9-4930-b139-cf835649703b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xgiEHjSc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1704931770863/704b5217-11f9-4930-b139-cf835649703b.png" alt="" width="800" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's say we wanted to use {team} instead of {tenant}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How would you do it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You may be tempted to drop ApiResource routes and manually create the routes so it's easy to name the placeholders such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('users/{user}/tenant', [UsersController::class, 'index'])-&amp;gt;name('users.tenant.index'); Route::post('users/{user}/tenant', [UsersController::class, 'store'])-&amp;gt;name('users.tenant.store');Route::get('users/{user}/tenant/{team}', [UsersController::class, 'show'])-&amp;gt;name('users.tenant.show');Route::match(['put', 'patch'], 'users/{user}/tenant/{team}', [UsersController::class, 'update'])-&amp;gt;name('users.tenant.update'); Route::delete('users/{user}/tenant/{team}', [UsersController::class, 'destroy'])-&amp;gt;name('users.tenant.destroy');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this will work, it would be better if an &lt;code&gt;ApiResouce&lt;/code&gt; can still be used.&lt;/p&gt;

&lt;p&gt;It can, instead of manually creating the routes we can rename the placeholders in two ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Route Parameter methods
&lt;/h2&gt;

&lt;p&gt;1) Using &lt;code&gt;parameter()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::apiResource('users.tenant', Users::class) -&amp;gt;parameter('tenant', 'team');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By setting &lt;code&gt;parameter&lt;/code&gt; we can set the name of the existing placeholder and give it a new name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B4QasQsD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1704932269598/742289fb-5603-408e-990b-4c7786df4fca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B4QasQsD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1704932269598/742289fb-5603-408e-990b-4c7786df4fca.png" alt="" width="800" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2) use &lt;code&gt;parameters()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::apiResource('users.tenant', Users::class)-&amp;gt;parameters(['users' =&amp;gt; 'author', 'tenant' =&amp;gt; 'team']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you want to name more than one placeholder use parameters and pass in an array.&lt;/p&gt;

&lt;p&gt;In this case, I've renamed &lt;code&gt;users&lt;/code&gt; to &lt;code&gt;author&lt;/code&gt; and &lt;code&gt;tenant&lt;/code&gt; to &lt;code&gt;team&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember when setting the first placeholder, it has to match the route name. In this case users. If user was set the {user} placeholder would not be changed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--02KA7KTS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1704932643787/37c5e2e6-81ed-4d75-b8e6-0a51632aee43.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--02KA7KTS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1704932643787/37c5e2e6-81ed-4d75-b8e6-0a51632aee43.png" alt="" width="800" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With either option, you can rename the placeholders whilst still using &lt;code&gt;ApiResource&lt;/code&gt; This helps keep your routes file clean.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>My process for writing Laravel packages</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Fri, 10 Nov 2023 20:51:07 +0000</pubDate>
      <link>https://forem.com/dcblog/my-process-for-writing-laravel-packages-2kko</link>
      <guid>https://forem.com/dcblog/my-process-for-writing-laravel-packages-2kko</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I was asked recently how I started when writing new Laravel packages.&lt;/p&gt;

&lt;p&gt;In this post I'm going to build a new package called &lt;code&gt;Laravel Tags&lt;/code&gt; it will take a string and replace placeholder tags kinda of like shortcodes.&lt;/p&gt;

&lt;p&gt;For example, have a blog post that contains tags [year] or [appName] and have them replaced in the content.&lt;/p&gt;

&lt;p&gt;So [year] would be replaced with the actual year. [appName] would be replaced with the Laravel application name.&lt;/p&gt;

&lt;p&gt;Its usage will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$content = "This post was made in [year] on the application [appName]";
$content Tags::get($content);
//$content would now contain This post was made in 2023 on the application Laravel

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How I organise my packages
&lt;/h2&gt;

&lt;p&gt;On my machine, I have a package folder. From here I make a folder for each package.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--afFWziHi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/imageupload/v1699632508007/45864c6a-bc25-4d90-b8d9-73532cbee18c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--afFWziHi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/imageupload/v1699632508007/45864c6a-bc25-4d90-b8d9-73532cbee18c.png" alt="Laravel Packages" width="211" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I then load these into my projects using &lt;code&gt;composer.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the require section load the package by its vendor name followed by the package name using &lt;code&gt;@dev&lt;/code&gt; to load the main version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"dcblogdev/laravel-tags": "@dev"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next in order to load this, I tell composer where to find the files locally using a repository path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"repositories": [
    {
        "type": "path",
        "url": "../../packages/laravel-tags"
    }
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, I need composer to look two folders above the current Laravel project.&lt;/p&gt;

&lt;p&gt;Now run &lt;code&gt;composer update&lt;/code&gt; to install the package into your Laravel project.&lt;/p&gt;

&lt;p&gt;Whats good about this approach is you can do this with third-party packages. This is useful as you can run tests and add features that you plan to raise pull requests for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build package structure
&lt;/h2&gt;

&lt;p&gt;You can use package starter kits such as &lt;a href="https://github.com/spatie/package-skeleton-laravel"&gt;https://github.com/spatie/package-skeleton-laravel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I tend to build packages from scratch or copy one of my existing packages and remove what I dont need.&lt;/p&gt;

&lt;p&gt;In this post, I'm imagining building a package called &lt;code&gt;laravel-tags&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The folder structure will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dcblogdev/laravel-tags"&gt;https://github.com/dcblogdev/laravel-tags&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JtXvtsSL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/imageupload/v1699632509341/c15d0343-0900-4149-be72-235d3c965019.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JtXvtsSL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/imageupload/v1699632509341/c15d0343-0900-4149-be72-235d3c965019.png" alt="Package Structure" width="674" height="782"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  license.md
&lt;/h2&gt;

&lt;p&gt;For the license, I use The MIT license which allows users to freely use, copy, modify, merge, publish, distribute, sublicense, and sell the software and its associated documentation.&lt;/p&gt;

&lt;p&gt;this file contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# The license

The MIT License (MIT)

Copyright (c) 2023 dcblogdev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  composer.json
&lt;/h2&gt;

&lt;p&gt;I'll start by adding a composer.json file that contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "dcblogdev/laravel-tags",
  "description": "A Laravel package for adding tags to your content",
  "license": "MIT",
  "authors": [
    {
      "name": "David Carr",
      "email": "dave@dcblog.dev",
      "homepage": "https://dcblog.dev"
    }
  ],
  "homepage": "https://github.com/dcblogdev/laravel-tags",
  "keywords": [
    "Laravel",
    "Tags"
  ],
  "require": {
    "php": "^8.1"
  },
  "require-dev": {
    "orchestra/testbench": "^8.0",
    "pestphp/pest": "^v2.24.2",
    "pestphp/pest-plugin-type-coverage": "^2.4",
    "laravel/pint": "^1.13",
    "mockery/mockery": "^1.6"
  },
  "autoload": {
    "psr-4": {
      "Dcblogdev\\Tags\\": "src/",
      "Dcblogdev\\Tags\\Tests\\": "tests"
    }
  },
  "autoload-dev": {
    "classmap": [
      "tests/TestCase.php"
    ]
  },
  "extra": {
    "laravel": {
      "providers": [
        "Dcblogdev\\Tags\\TagsServiceProvider"
      ],
      "aliases": {
        "Tags": "Dcblogdev\\Tags\\Facades\\Tags"
      }
    }
  },
  "config": {
    "allow-plugins": {
      "pestphp/pest-plugin": true
    }
  },
  "scripts": {
    "pest": "vendor/bin/pest --parallel",
    "pest-coverage": "vendor/bin/pest --coverage",
    "pest-type": "vendor/bin/pest --type-coverage",
    "pint": "vendor/bin/pint"
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Breaking this down, first, the package needs a name in the convention of vendor name and package name &lt;code&gt;dcblogdev/laravel-tags&lt;/code&gt; in my case the vendor name is &lt;code&gt;dcblogdev&lt;/code&gt; and the package name is &lt;code&gt;laravel-tags&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In a require-dev section, I import these third-party packages for development only.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;testbench package allows you to use Laravel conventions for testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pest is my testing framework of choice&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pint to apply styling conventions to my codebase.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"orchestra/testbench": "^8.0",
"pestphp/pest": "^v2.24.2",
"pestphp/pest-plugin-type-coverage": "^2.4",
"laravel/pint": "^1.13",
"mockery/mockery": "^1.6"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside autoload to any folders that you want composer to autoload. I have a folder called &lt;code&gt;src&lt;/code&gt; which will contain the classes and &lt;code&gt;tests&lt;/code&gt; for all the tests&lt;/p&gt;

&lt;p&gt;One important aspect of this package name I'll often give the package a nickname to use so instead of using &lt;code&gt;laravel-tags&lt;/code&gt; I'll use &lt;code&gt;tags&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I'll do this by using &lt;code&gt;Tags&lt;/code&gt; in any class namespaces and creating an alias:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"extra": {
  "laravel": {
    "providers": [
      "Dcblogdev\\Tags\\TagsServiceProvider"
    ],
    "aliases": {
      "Tags": "Dcblogdev\\Tags\\Facades\\Tags"
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tests
&lt;/h2&gt;

&lt;p&gt;Now I will create 2 folders:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;src&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tests&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Inside tests I'll create these files:&lt;/p&gt;

&lt;p&gt;TestCase.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace Dcblogdev\Tags\Tests;

use Dcblogdev\Tags\TagsServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
    protected function getPackageProviders($app)
    {
        return [
            TagsServiceProvider::class,
        ];
    }

    protected function getEnvironmentSetUp($app)
    {
        $app['config']-&amp;gt;set('database.default', 'mysql');
        $app['config']-&amp;gt;set('database.connections.mysql', [
            'driver' =&amp;gt; 'sqlite',
            'host' =&amp;gt; '127.0.0.1',
            'database' =&amp;gt; ':memory:',
            'prefix' =&amp;gt; '',
        ]);
    }

    protected function defineDatabaseMigrations()
    {
        $this-&amp;gt;loadLaravelMigrations();

        $this-&amp;gt;loadMigrationsFrom(dirname( __DIR__ ).'/src/database/migrations');
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The methods &lt;code&gt;getEnviromentSetup&lt;/code&gt; and &lt;code&gt;defineDatabaseMigrations&lt;/code&gt; are not needed by default. They are only required if you need to use a database and load migrations.&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;getPackageProviders&lt;/code&gt; the main package service provider is loaded.&lt;/p&gt;

&lt;p&gt;Now create a file called Pest.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

use Dcblogdev\Tags\Tests\TestCase;

uses(TestCase::class)-&amp;gt;in( __DIR__ );

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file calls a &lt;code&gt;uses()&lt;/code&gt; function to load the &lt;code&gt;testcase::class&lt;/code&gt; I'll use &lt;code&gt;__DIR__&lt;/code&gt; inside the &lt;code&gt;in()&lt;/code&gt; method to make Pest use the TestCase class in the root of this directory.&lt;/p&gt;

&lt;p&gt;Inside the package root create a phpunit.xml file containing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
&amp;gt;
  &amp;lt;source&amp;gt;
    &amp;lt;include&amp;gt;
      &amp;lt;directory suffix=".php"&amp;gt;src/&amp;lt;/directory&amp;gt;
    &amp;lt;/include&amp;gt;
  &amp;lt;/source&amp;gt;
  &amp;lt;testsuites&amp;gt;
    &amp;lt;testsuite name="Test"&amp;gt;
      &amp;lt;directory suffix="Test.php"&amp;gt;./tests&amp;lt;/directory&amp;gt;
    &amp;lt;/testsuite&amp;gt;
  &amp;lt;/testsuites&amp;gt;
  &amp;lt;php&amp;gt;
    &amp;lt;env name="APP_KEY" value="base64:2fl+Ktv64dkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/&amp;gt;
  &amp;lt;/php&amp;gt;
&amp;lt;/phpunit&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sets the location for tests to read from, it's rare this file will need to be changed from this default.&lt;/p&gt;

&lt;p&gt;I set an &lt;code&gt;APP_KEY&lt;/code&gt; standard for running tests, its value is not important.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pint
&lt;/h2&gt;

&lt;p&gt;To set up find create a folder in the package root called pint.json I use the Laravel preset:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ 
    "preset": "laravel" 
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ReadMe
&lt;/h2&gt;

&lt;p&gt;inside the project root create a file called readme.md to document the project, I typically use this format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Laravel Tags

Explain what the package does.

# Documentation and install instructions 
[https://docs.dcblog.dev/docs/laravel-tags](https://docs.dcblog.dev/docs/laravel-tags)

## Change log

Please see the [changelog][3] for more information on what has changed recently.

## Contributing

Contributions are welcome and will be fully credited.

Contributions are accepted via Pull Requests on [Github][4].

## Pull Requests

- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0][5]. Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

## Security

If you discover any security related issues, please email dave@dcblog.dev email instead of using the issue tracker.

## License

license. Please see the [license file][6] for more information.






&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ServiceProvider
&lt;/h2&gt;

&lt;p&gt;Inside src create the service provider in my case TagsServiceProvider.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace Dcblogdev\Tags;

use Illuminate\Support\ServiceProvider;

class TagsServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        $this-&amp;gt;configurePublishing();
    }

    public function configurePublishing(): void
    {
        if (! $this-&amp;gt;app-&amp;gt;runningInConsole()) {
            return;
        }

        $this-&amp;gt;publishes([
            __DIR__.'/../config/tags.php' =&amp;gt; config_path('tags.php'),
        ], 'config');
    }

    public function register(): void
    {
        $this-&amp;gt;mergeConfigFrom( __DIR__.'/../config/tags.php', 'tags');

        // Register the service the package provides.
        $this-&amp;gt;app-&amp;gt;singleton('tags', function () {
            return new Tags;
        });
    }

    public function provides(): array
    {
        return ['tags'];
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the boot method list the methods, to begin with I only have one called configurePublishing This will publish any files defined in this page and will only run when the class is ran from a CLI&lt;/p&gt;

&lt;p&gt;The register method allows a published config file to be merged in with a local &lt;code&gt;./config/tags.php&lt;/code&gt; config file&lt;/p&gt;

&lt;p&gt;And set up the main class to be instantiated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Facades
&lt;/h2&gt;

&lt;p&gt;If you want to make use of facade ie have static called to a class that would normally be instantiated.&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;src&lt;/code&gt; create a folder called &lt;code&gt;facades&lt;/code&gt; and your class such as &lt;code&gt;Tags.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace Dcblogdev\Tags\Facades;

use Illuminate\Support\Facades\Facade;

class Tags extends Facade
{
    protected static function getFacadeAccessor(): string
    {
        return 'tags';
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows for calling the package's class and method in one show:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tags::get();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Notice I did not have to instantiate a class in order to call its methods.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Building the Tags package
&lt;/h1&gt;

&lt;p&gt;Now I have the basic structure in place I can concentrate on building the package functionality.&lt;/p&gt;

&lt;p&gt;I will now create a &lt;code&gt;Tags&lt;/code&gt; class this is the main class that will provide the modify any content I provide to it.&lt;/p&gt;

&lt;p&gt;Inside &lt;code&gt;src&lt;/code&gt; create a new class call it &lt;code&gt;Tags.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Set the namespace and class definition.&lt;/p&gt;

&lt;p&gt;Next, I'll create a method called get that will accept a string of content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace Dcblogdev\Tags;

class Tags
{
    public function get(string $content): string
    {
        //Current year
        $content = str_replace('[year]', date('Y'), $content);

        //Name of website
        $content = str_replace('[appName]', config('app.name'), $content);

        return $content;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;code&gt;get&lt;/code&gt; I use str_replace to replace tags with a value and finally return the modified content string.&lt;/p&gt;

&lt;p&gt;This is very simple on purpose, lets add another tag &lt;code&gt;[youtube url-here]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This youtube tag expects a URL of a video to be passed, and also a width and height can be passed.&lt;/p&gt;

&lt;p&gt;This will return the embed code needed to play a youtube video.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//youtube embeds
$content = preg_replace_callback("(\[youtube (.*?)])is", function ($matches) {
    $params = $this-&amp;gt;clean($matches);

    //if key exits use it
    $video = $params['//www.youtube.com/watch?v'];
    $width = ($params['width'] ?? '560');
    $height = ($params['height'] ?? '360');

    return "&amp;lt;iframe width='$width' height='$height' src='//www.youtube.com/embed/$video' frameborder='0' allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;";
}, $content);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this is getting a little more interesting let's add our tests.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;code&gt;TagsTest.php&lt;/code&gt; inside the &lt;code&gt;tests&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Here's 2 simple tests that confirm [year] and [appName] get replaced as expected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Dcblogdev\Tags\Facades\Tags;

test('[year] sets the year', function () {
    $year = date('Y');
    $response = Tags::get('[year]');

    expect($response)-&amp;gt;toBe($year);
});

test('[appName] sets the name of the app', function () {
    $response = Tags::get('[appName]');

    expect($response)-&amp;gt;toBe('Laravel');
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next here is a test to confirm a YouTube video can be embedded:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test('can embed youtube videos', function () {
    $response = Tags::get('[youtube https://www.youtube.com/watch?v=cAzwfFPmghY]');

    expect($response)
        -&amp;gt;toBe("&amp;lt;iframe width='560' height='360' src='//www.youtube.com/embed/cAzwfFPmghY' frameborder='0' allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;");
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This requires more than a simple find and replace based on a tag now. The tag now has a wildcard so [youtube &lt;a href="https://www.youtube.com/watch?v=ehLx-jO1LF0&amp;amp;t=10s"&gt;https://www.youtube.com/watch?v=ehLx-jO1LF0&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;needs to be extract the id of the video from the url, also needs to handle other parms being passed.&lt;/p&gt;

&lt;p&gt;Looking over the snippet for YouTube&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//youtube embeds
$content = preg_replace_callback("(\[youtube (.*?)])is", function ($matches) {
    $params = $this-&amp;gt;clean($matches);

    //if key exits use it
    $video = $params['//www.youtube.com/watch?v'];
    $width = ($params['width'] ?? '560');
    $height = ($params['height'] ?? '360');

    return "&amp;lt;iframe width='$width' height='$height' src='//www.youtube.com/embed/$video' frameborder='0' allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;";
}, $content);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice this is calling a method called clean and passing in an array of $matches&lt;/p&gt;

&lt;p&gt;Open The &lt;code&gt;Tags.php&lt;/code&gt; class&lt;/p&gt;

&lt;p&gt;Let's add 2 private methods &lt;code&gt;clean&lt;/code&gt; and &lt;code&gt;removeCharsFromString&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private function clean(array|string $data): array
{
    // Check if $data is an array and extract the string to be processed.
    $stringToProcess = is_array($data) &amp;amp;&amp;amp; isset($data[1]) ? $data[1] : $data;

    // Ensure that the stringToProcess is actually a string.
    if (!is_string($stringToProcess)) {
        // Handle error or return an empty array
        return [];
    }

    $parts = explode(' ', $stringToProcess);
    $params = [];

    foreach ($parts as $part) {
        if (!empty($part)) {
            if (str_contains($part, '=')) {
                [$name, $value] = explode('=', $part, 2);
                $value = $this-&amp;gt;removeCharsFromString($value);
                $name = $this-&amp;gt;removeCharsFromString($name);
                $params[$name] = $value;
            } else {
                $params[] = $this-&amp;gt;removeCharsFromString($part);
            }
        }
    }

    return $params;
}

private function removeCharsFromString(string $value): string
{
    $search = ['http:', 'https:', '&amp;amp;quot;', '&amp;amp;rdquo;', '&amp;amp;rsquo;', ' '];
    return str_replace($search, '', $value);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the clean method takes an array or string and will removes anything inside &lt;code&gt;removeCharsFromString&lt;/code&gt; and will separate &lt;code&gt;something=value&lt;/code&gt; into &lt;code&gt;['something' = 'value']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Adding a few more tests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test('can embed youtube videos', function () {
    $response = Tags::get('[youtube https://www.youtube.com/watch?v=cAzwfFPmghY]');

    expect($response)
        -&amp;gt;toBe("&amp;lt;iframe width='560' height='360' src='//www.youtube.com/embed/cAzwfFPmghY' frameborder='0' allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;");
});

test('can embed youtube videos with width and height', function () {
    $response = Tags::get('[youtube https://www.youtube.com/watch?v=cAzwfFPmghY width=100 height=100]');

    expect($response)
        -&amp;gt;toBe("&amp;lt;iframe width='100' height='100' src='//www.youtube.com/embed/cAzwfFPmghY' frameborder='0' allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;");
});

test('can embed youtube videos with height', function () {
    $response = Tags::get('[youtube https://www.youtube.com/watch?v=cAzwfFPmghY height=100]');

    expect($response)
        -&amp;gt;toBe("&amp;lt;iframe width='560' height='100' src='//www.youtube.com/embed/cAzwfFPmghY' frameborder='0' allowfullscreen&amp;gt;&amp;lt;/iframe&amp;gt;");
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These will confirm all tags work as expected.&lt;/p&gt;

&lt;h1&gt;
  
  
  Publish to GitHub
&lt;/h1&gt;

&lt;p&gt;Create a new repo on Github &lt;a href="https://github.com/new"&gt;https://github.com/new&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter a repository name in my case &lt;code&gt;laravel-tags&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I don't change any other settings and click &lt;code&gt;Create Repository&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I don't want anything committed to the repo, I want it empty so I can upload it from my machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y3RdM0rG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699718997534/d906ad9a-d976-47bf-8e6b-95ee127aceaf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y3RdM0rG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699718997534/d906ad9a-d976-47bf-8e6b-95ee127aceaf.png" alt="" width="800" height="663"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Git setup
&lt;/h2&gt;

&lt;p&gt;Now go back to your codebase and initialse git in the terminal at the project root type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will initalise GIT,&lt;/p&gt;

&lt;p&gt;Next, add and commit your files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m 'first commit

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can link your local GIT repo to the GitHub repo replace &lt;code&gt;dcblogdev&lt;/code&gt; with your GitHub username and &lt;code&gt;laravel-tags&lt;/code&gt; with the name of your package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote add origin git@github.com:dcblogdev/laravel-tags.git

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Push up your code using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push -u origin main

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From now on you can push up using just &lt;code&gt;git push&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a release on GitHub
&lt;/h2&gt;

&lt;p&gt;Once you're happy with the package and ready to publish a release follow these steps:&lt;/p&gt;

&lt;p&gt;To to the releases page of your project on GitHub and click &lt;code&gt;create release&lt;/code&gt; or use the following URL (replace &lt;a href="https://github.com/dcblogdev/laravel-tags/releases/new"&gt;dcblogdev/laravel-tags&lt;/a&gt; with your version)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dcblogdev/laravel-tags/releases/new"&gt;https://github.com/dcblogdev/laravel-tags/releases/new&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter a tag I prefer to start my packages with &lt;code&gt;v1.0.0&lt;/code&gt; then enter your release notes and click attach binaries to upload a zip of your package and finally press &lt;code&gt;Publish release&lt;/code&gt; This will publish a new release.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ROt9FRh5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699719714512/1f76fcb0-8769-4095-8ada-50539275c178.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ROt9FRh5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699719714512/1f76fcb0-8769-4095-8ada-50539275c178.png" alt="" width="800" height="910"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using GitHub CLI
&lt;/h3&gt;

&lt;p&gt;This is a long process, I prefer to create a release from the terminal using GitHub's CLI read more about it at &lt;a href="https://cli.github.com/"&gt;https://cli.github.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this is installed I create a release with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gh release create v1.0.0

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Press enter and a prompt will ask you the following questions.&lt;/p&gt;

&lt;p&gt;Go through and then select Publish release press enter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UQW7_0rE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699720095037/0a63397b-b474-4a43-a88e-20b6545c2a52.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UQW7_0rE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699720095037/0a63397b-b474-4a43-a88e-20b6545c2a52.png" alt="" width="800" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now a release has been created on GitHub&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--thHlOOZR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699720145748/896f79f4-32c5-4a67-8209-375fd52d143d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--thHlOOZR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699720145748/896f79f4-32c5-4a67-8209-375fd52d143d.png" alt="" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Publish package
&lt;/h1&gt;

&lt;p&gt;In order to be able to install the package on your applications and not have to tell composer where to find the package from a local folder the package can be set-up on a website called Packagist &lt;a href="https://packagist.org/packages/submit"&gt;https://packagist.org/packages/submit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter the URL to your package on GitHub and press Check. If there are other packages with the same name Packagist will show them, if you're sure you want to publish your version press submit to continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qGj_zg-f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699718521172/f4e213cd-102c-4d3d-b091-2cb37bd38dbf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qGj_zg-f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699718521172/f4e213cd-102c-4d3d-b091-2cb37bd38dbf.png" alt="" width="800" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once completed the page will show your package:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1Y6-1aMX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699718816275/3a3afea6-48c6-4eab-8ef2-b210fa13f83a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1Y6-1aMX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1699718816275/3a3afea6-48c6-4eab-8ef2-b210fa13f83a.png" alt="" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order for the package to be installable ensure you've issued a release on GitHub otherwise you may get this error:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Could not find a version of package dcblogdev/laravel-tags matching your minimum-stability (stable). Require it with&lt;br&gt;&lt;br&gt;
an explicit version constraint allowing its desired stability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;For more details on Laravel Package development read &lt;a href="https://www.laravelpackage.com/"&gt;https://www.laravelpackage.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>packages</category>
    </item>
    <item>
      <title>Running HTTP requests in PhpStorm</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Tue, 10 Oct 2023 19:48:38 +0000</pubDate>
      <link>https://forem.com/dcblog/running-http-requests-in-phpstorm-56oh</link>
      <guid>https://forem.com/dcblog/running-http-requests-in-phpstorm-56oh</guid>
      <description>&lt;p&gt;Running API tests you may be tempted to open PostMan or another API client, did you know, you can run HTTP requests from inside PhpStorm directly?&lt;/p&gt;

&lt;p&gt;When developing a new API, you will want to run the API endpoints. Being able to run them without leaving PhpStorm speeds up your workflow.&lt;/p&gt;

&lt;p&gt;To start a request file, you can either select a &lt;code&gt;HTTP Request&lt;/code&gt; file when using the new file or create a new scratch file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u6pC2P65--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696958133738/b3b55ccc-3b13-4674-9fa4-ef42178aa824.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u6pC2P65--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696958133738/b3b55ccc-3b13-4674-9fa4-ef42178aa824.png" alt="" width="800" height="679"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a scratch file select the &lt;code&gt;HTTP Request&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1OdJ7VfL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696958227364/9573ce5a-8175-4566-a11b-6de43f648c20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1OdJ7VfL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696958227364/9573ce5a-8175-4566-a11b-6de43f648c20.png" alt="" width="736" height="728"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In either case a file ending in &lt;code&gt;.http&lt;/code&gt; for example &lt;code&gt;scratch.http&lt;/code&gt; You can name the file anything you like and have multiple files.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The difference between having &lt;code&gt;.http&lt;/code&gt; files in your projects and a scratch file is a scratch file is only stored in your &lt;code&gt;.idea&lt;/code&gt; folder and not inside your project files, (outside of version control.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Demo API
&lt;/h2&gt;

&lt;p&gt;Throughout this post, I will be using a sample public API &lt;a href="https://reqres.in/"&gt;https://reqres.in/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Request Format
&lt;/h2&gt;

&lt;p&gt;To create a request enter the type of request (GET, POST, PUT, PATCH, DELETE) followed by the URL to fetch.&lt;/p&gt;

&lt;p&gt;You can also set headers by typing them in the format of &lt;code&gt;key:value&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you need to pass through a body then type that out with a line break in a json format.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;###&lt;/code&gt; to separate each request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Method Request-URI HTTP-Version
Header-field: Header-value

Request-Body
###
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Get Request
&lt;/h2&gt;

&lt;p&gt;To create a &lt;code&gt;GET&lt;/code&gt; request type GET followed by the URL to fetch.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can also set headers by typing them in the format of &lt;code&gt;key:value&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET https://reqres.in/api/users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Press the play button to the left of the request to run. This executes the request and shows the API response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FM-nZkrX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696966405315/afac14bf-0a55-47cc-a4c0-0600c563377c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FM-nZkrX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1696966405315/afac14bf-0a55-47cc-a4c0-0600c563377c.png" alt="" width="800" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  POST Request
&lt;/h2&gt;

&lt;p&gt;In this example, I will create a new user by sending a POST request to the URL with a payload containing a name and job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST https://reqres.in/api/users

{
    "name": "morpheus",
    "job": "leader"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PATCH/PUT Request
&lt;/h2&gt;

&lt;p&gt;In this example, I will update a user by sending a PATCH request to the URL with a payload containing a name and job.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PUT can also be used&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PATCH https://reqres.in/api/users/2

{
    "name": "joe"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Delete Request
&lt;/h2&gt;

&lt;p&gt;In this example, I will delete a user by sending a DELETE request to the URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE https://reqres.in/api/users/2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Authorization header
&lt;/h2&gt;

&lt;p&gt;If you need to authenticate with an API say in the format of a bearer token this is done by passing in an authorization header:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
GET http://domain.com/v1.0/contacts
Accept: application/json
Content-Type: application/json
Authorization: Bearer 45|vAc8...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more details read the &lt;a href="https://www.jetbrains.com/help/phpstorm/http-client-in-product-code-editor.html"&gt;official docs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>phpstorm</category>
      <category>http</category>
      <category>requests</category>
    </item>
    <item>
      <title>Extracting a time into select menus with PHP</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Thu, 05 Oct 2023 23:00:00 +0000</pubDate>
      <link>https://forem.com/dcblog/extracting-a-time-into-select-menus-with-php-2la9</link>
      <guid>https://forem.com/dcblog/extracting-a-time-into-select-menus-with-php-2la9</guid>
      <description>&lt;p&gt;When storing time in a database, often I'll store this in a time format such as &lt;code&gt;14:25:00&lt;/code&gt; When populating this in a form I'll either use a time picker or use a plain select menu a separate menu for each element.&lt;/p&gt;

&lt;p&gt;That begs the question how do you separate the time into seperate elements?&lt;/p&gt;

&lt;p&gt;Making use of &lt;code&gt;explode&lt;/code&gt; can work. Exploding where there a &lt;code&gt;:&lt;/code&gt; so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;explode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;':'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'14:25:00'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would result in an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"14"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"25"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use an element you can refer to its index for example &lt;code&gt;$parts[0]&lt;/code&gt; would contain &lt;code&gt;14&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using them by their index is fine to do but kinda messy.&lt;/p&gt;

&lt;p&gt;I prefer to give each element a name using the &lt;code&gt;list&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$hours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$minutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$seconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;explode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;':'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'14:25:00'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using &lt;code&gt;list()&lt;/code&gt; passing an array to it will allow you to name each index of the array in the order listed.&lt;/p&gt;

&lt;p&gt;Putting this into practice I like to define the null versions before using list to ensure the variables will exist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$estimatedTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mo"&gt;00&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$estimatedHour&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$estimatedMinute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$estimatedSeconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$estimatedHour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$estimatedMinute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$estimatedSeconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;explode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;':'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$estimatedTime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have the variables that can be used in select menus:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;note I'm using Blade syntax here for readability.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"estimatedTime_hour"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hours&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    @foreach(range(0,23) as $hour)
        &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"{{ $hour }}"&lt;/span&gt; &lt;span class="err"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;old&lt;/span&gt;&lt;span class="err"&gt;('&lt;/span&gt;&lt;span class="na"&gt;estimatedTime_hour&lt;/span&gt;&lt;span class="err"&gt;',&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="na"&gt;estimatedHour) =&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="na"&gt;hour&lt;/span&gt; &lt;span class="err"&gt;?&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="na"&gt;selected=&lt;/span&gt;&lt;span class="s"&gt;selected'&lt;/span&gt; &lt;span class="na"&gt;:&lt;/span&gt; &lt;span class="err"&gt;''&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{ $hour }}&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    @endforeach
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"estimatedTime_minute"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Minutes&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    @foreach(range(0,59) as $minute)
        &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"{{ $minute }}"&lt;/span&gt; &lt;span class="err"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;old&lt;/span&gt;&lt;span class="err"&gt;('&lt;/span&gt;&lt;span class="na"&gt;estimatedTime_minute&lt;/span&gt;&lt;span class="err"&gt;',&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="na"&gt;estimatedMinute) =&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="na"&gt;hour&lt;/span&gt; &lt;span class="err"&gt;?&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="na"&gt;selected=&lt;/span&gt;&lt;span class="s"&gt;selected'&lt;/span&gt; &lt;span class="na"&gt;:&lt;/span&gt; &lt;span class="err"&gt;''&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{ $minute }}&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    @endforeach
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"estimatedTime_second"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Seconds&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    @foreach(range(0,59) as $second)
        &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"{{ $second }}"&lt;/span&gt; &lt;span class="err"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;old&lt;/span&gt;&lt;span class="err"&gt;('&lt;/span&gt;&lt;span class="na"&gt;estimatedTime_second&lt;/span&gt;&lt;span class="err"&gt;',&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="na"&gt;estimatedSeconds) =&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="na"&gt;hour&lt;/span&gt; &lt;span class="err"&gt;?&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="na"&gt;selected=&lt;/span&gt;&lt;span class="s"&gt;selected'&lt;/span&gt; &lt;span class="na"&gt;:&lt;/span&gt; &lt;span class="err"&gt;''&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{ $second }}&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    @endforeach
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each time segment I have a separate select menu. Each selection uses a &lt;code&gt;range&lt;/code&gt; function to generate an array of items. For example, &lt;code&gt;range(0,23)&lt;/code&gt; will return an array containing all numbers between 0 and 23.&lt;/p&gt;

&lt;p&gt;To ensure the time segment selects the correct array index inside the loop compare the the row against the time segment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$estimatedSeconds&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nv"&gt;$hour&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When they match then print selected=selected to ensure the menu selects that time segment.&lt;/p&gt;

&lt;p&gt;This is a simple but powerful pattern.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Vue show and hide form elements</title>
      <dc:creator>David Carr</dc:creator>
      <pubDate>Wed, 04 Oct 2023 07:07:20 +0000</pubDate>
      <link>https://forem.com/dcblog/vue-show-and-hide-form-elements-4ild</link>
      <guid>https://forem.com/dcblog/vue-show-and-hide-form-elements-4ild</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When working with forms, it is common to display or conceal various fields based on the options selected.&lt;/p&gt;

&lt;p&gt;I'll use Vue to conditionally show or hide form elements.&lt;/p&gt;

&lt;p&gt;In this example, I'm going to use an SFC (Single File Component) for ease of us. You'll see this in full in the putting it all together section below.&lt;/p&gt;

&lt;p&gt;For this to work it does not need to use a SFC, the HTML and JS will work when used in traditional setups i.e. having JS in javascript blocks and the HTML in a page's body.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the form elements
&lt;/h2&gt;

&lt;p&gt;Here I have 2 select elements, imagine the &lt;code&gt;insurance type&lt;/code&gt; should only be displayed if the answer to &lt;code&gt;Do you want insurance?&lt;/code&gt; was &lt;code&gt;Yes&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Do you want insurance?&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Yes&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;No&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Insurance Type&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Travel&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Binding Form Elements
&lt;/h2&gt;

&lt;p&gt;First, we need to create variables to bind the elements. Inside a script tag define a const for each element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="nx"&gt;setup&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;insurance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;insuranceType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;ref&lt;/code&gt; is imported from Vue, as by default, no directives are available for variables.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ref()&lt;/code&gt; allows the variable to be reactive, meaning that Vue will monitor it and re-render the action whenever its state changes.&lt;/p&gt;

&lt;p&gt;To bind the elements with the variables use &lt;code&gt;v-model&lt;/code&gt; this creates a 2-way binding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Do you want insurance?&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Yes&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;No&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Insurance Type&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Travel&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Show / Hide elements select
&lt;/h2&gt;

&lt;p&gt;Now to only show the &lt;code&gt;insurance type&lt;/code&gt; when the question &lt;code&gt;Do you want insurance?&lt;/code&gt; is set to &lt;code&gt;Yes&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This can be accomplished by wrapping the second select in a div and using a &lt;code&gt;v-show&lt;/code&gt; directive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"insurance === 'Yes'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;v-show&lt;/code&gt; takes a condition that has a truthy value, this must equate to true or false. When true the body of the div will be displayed, otherwise its not rendered to the page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Do you want insurance?&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Yes&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;No&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"insurance === 'Yes'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Insurance Type&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Travel&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Taking this a step further have additional select elements that only appear when an &lt;code&gt;insurance type&lt;/code&gt; is selected.&lt;/p&gt;

&lt;p&gt;I have 2 divs that will only render when the &lt;code&gt;insuranceType&lt;/code&gt; value matches either &lt;code&gt;Home&lt;/code&gt; or &lt;code&gt;Travel&lt;/code&gt; which matches the option selected from the &lt;code&gt;Insurance Type&lt;/code&gt; select menu.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Insurance Type&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Travel&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType === 'Home'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Home Details&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType === 'Travel'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Travel Details&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Show or hide checkbox
&lt;/h2&gt;

&lt;p&gt;What about when you have a checkbox that should render markup but only when it's checked, how may that work?&lt;/p&gt;

&lt;p&gt;Let's create a input element with a type of checkbox, we'll v-model bind a variable called &lt;code&gt;addAComment&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add a comment?&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update the Javascript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addAComment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add a &lt;code&gt;v-model&lt;/code&gt; to the &lt;code&gt;checkbox&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, create a div with a &lt;code&gt;v-show&lt;/code&gt; this time it only needs the variable, when the checkbox is set it will have a value otherwise it will not. This is enough to activate &lt;code&gt;v-show&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add a comment?&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"comment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Comment&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;textarea&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"comment"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"comment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/textarea&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
    {{ comment }}
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Putting it all together
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;insurance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;insuranceType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addAComment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;comment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Do you want insurance?&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"insurance"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Yes&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;No&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"insurance === 'Yes'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Insurance Type&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Select&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;option&amp;gt;&lt;/span&gt;Travel&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType === 'Home'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Home Details&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"insuranceType === 'Travel'"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Travel Details&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add a comment?&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"addAComment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"comment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Comment&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;textarea&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"comment"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"comment"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/textarea&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
      {{ comment }}
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;label&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;How v-show differs from v-if&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;It's important to note that &lt;code&gt;v-show&lt;/code&gt; and &lt;code&gt;v-if&lt;/code&gt; serve similar purposes in controlling element visibility, but they have some key differences:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;v-show&lt;/code&gt;: The element is always rendered in the DOM, but its CSS display property is toggled between none and the original value (e.g., block, inline, etc.) to show or hide it. This makes it more efficient for elements that frequently toggle between visible and hidden states.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;v-if&lt;/code&gt;: The element is conditionally created or destroyed in the DOM. When the condition is false, the element is completely removed from the DOM. This can be more efficient when you have elements that are rarely used or have complex rendering logic, as they are not in the DOM until needed.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
