<?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: Isra Skyler</title>
    <description>The latest articles on Forem by Isra Skyler (@isra_skyler).</description>
    <link>https://forem.com/isra_skyler</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%2F1169657%2F807b6cb5-a9ba-4863-85d4-970881dd267f.jpg</url>
      <title>Forem: Isra Skyler</title>
      <link>https://forem.com/isra_skyler</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/isra_skyler"/>
    <language>en</language>
    <item>
      <title>Crush Bugs with Laravel Contract-Based Testing</title>
      <dc:creator>Isra Skyler</dc:creator>
      <pubDate>Fri, 03 Nov 2023 12:27:26 +0000</pubDate>
      <link>https://forem.com/isra_skyler/crush-bugs-with-laravel-contract-based-testing-3oo1</link>
      <guid>https://forem.com/isra_skyler/crush-bugs-with-laravel-contract-based-testing-3oo1</guid>
      <description>&lt;p&gt;As an experienced Laravel developer at &lt;a href="https://hybridwebagency.com/"&gt;Hybrid Web Agency&lt;/a&gt;, reliability is core to every project I take on. While testing is mandatory, traditional methods sometimes fall short on complex work with changing requirements.&lt;br&gt;
That's why I've adopted contract-based testing to validate new features and squash bugs before delivery. By defining intended behaviors and expected outputs, contracts ensure my code works as planned - now and later.&lt;br&gt;
This focus on quality has paid dividends for clients. Budgets hold steady as rework decreases. Users enjoy seamless experiences and confidence, with freedom to adjust scope down the line.&lt;br&gt;
In this post, I'll demonstrate how contract testing improved one application. From setup to maintenance, see how shifting left on quality boosts deliverables and strengthens client relationships. Once you know the process - You will be much more confident before you &lt;a href="https://hybridwebagency.com/everett-wa/custom-laravel-development-services/"&gt;Hire Laravel Developers in Everett&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  What are Contract Tests?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://laravel.com/docs/10.x/contracts"&gt;Contract testing&lt;/a&gt; is an approach that validates public APIs by specifying expected app behaviors rather than implementation details. Unlike unit or integration tests, contracts asserts code works as intended now and handles future unknown changes. &lt;/p&gt;

&lt;p&gt;Contracts define just the public methods and properties of a class - its "contract" with external users. Tests then exercise this public API through realistic usage scenarios and assertions. This separates the "contract" from how it's coded internally.&lt;/p&gt;

&lt;p&gt;Some key benefits include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Focused on critical behaviors:&lt;/strong&gt; Tests validate crucial expected inputs/outputs rather than testing all code paths. This focuses on important use cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stable over time:&lt;/strong&gt; Contracts act as documentation and guards against regressions from code changes. Tests still pass as code evolves if output contracts are met. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reader-focused documentation:&lt;/strong&gt; Contracts double as living documentation developers can reference to understand how to use classes properly without digging through code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a simple example contract class for validating a common "CountAll" method:&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="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;PostRepositoryContract&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;countAll&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;;&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;PostRepositoryContractTest&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;TestCase&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;testCountAllReturnsInteger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nv"&gt;$mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mockery&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PostRepositoryContract&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nv"&gt;$count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$mock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;countAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;assertInternalType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'integer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$count&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 contract asserts the method returns an integer without concerning how counting is implemented - keeping tests stable over time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Contract Tests in Laravel
&lt;/h2&gt;

&lt;p&gt;Setting up contract tests in Laravel involves just a few simple steps to get your environment configured.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Necessary Packages
&lt;/h3&gt;

&lt;p&gt;The main package needed is PHPUnit. Run &lt;code&gt;composer require --dev phpunit/phpunit&lt;/code&gt; to add it as a development dependency. &lt;/p&gt;

&lt;p&gt;You'll also want helper packages like Mockery for generating test doubles. &lt;code&gt;composer require --dev mockery/mockery&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring the Testing Environment
&lt;/h3&gt;

&lt;p&gt;Add &lt;code&gt;phpunit.xml&lt;/code&gt; to your project root with the basic configuration. This tells PHPUnit where to find your tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;phpunit&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;testsuites&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;testsuite&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"App Tests"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;directory&amp;gt;&lt;/span&gt;tests&lt;span class="nt"&gt;&amp;lt;/directory&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/testsuite&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/testsuites&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/phpunit&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Generating a Basic Test Stub
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;tests&lt;/code&gt; directory and a sample test file like &lt;code&gt;Feature/UserTest.php&lt;/code&gt;. Import necessary classes and traits:&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;use&lt;/span&gt; &lt;span class="nc"&gt;PHPUnit\Framework\TestCase&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;FeatureTest&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;TestCase&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;setUp&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="c1"&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;testExample&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;Now your environment is ready to start writing focused contract tests!&lt;/p&gt;

&lt;h2&gt;
  
  
  A Real-World Contract Test Example
&lt;/h2&gt;

&lt;p&gt;To demonstrate contract testing in action, let's look at a real-world example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Choosing a Class to Test
&lt;/h3&gt;

&lt;p&gt;For this example, we'll focus on a core Repository class that interacts with the database, such as a PostRepository. It retrieves, creates, updates posts and is crucial to our app's functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining the Public API Contract
&lt;/h3&gt;

&lt;p&gt;First, we define the public methods and properties the repository exposes with an interface:&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="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;PostRepositoryInterface&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;all&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;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// etc&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Writing Test Assertions
&lt;/h3&gt;

&lt;p&gt;Next, we create tests that exercise this interface through common usage scenarios:&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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;testAllReturnsCollection&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;$mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mockery&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PostRepositoryInterface&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$mock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;assertInstanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collection&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$posts&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;testCreateStoresPost&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;$mock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mockery&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PostRepositoryInterface&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nv"&gt;$mock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;shouldReceive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'create'&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;once&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nv"&gt;$mock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Test'&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;h3&gt;
  
  
  Running Tests
&lt;/h3&gt;

&lt;p&gt;Now simply run &lt;code&gt;phpunit&lt;/code&gt;! These contract tests validate our code's public behaviors independently of implementation details.&lt;/p&gt;

&lt;p&gt;Let's see how this approach improves our code quality over time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Contracts to Test
&lt;/h2&gt;

&lt;p&gt;There are some key areas of a typical Laravel application that benefit most from contract testing:&lt;/p&gt;

&lt;h3&gt;
  
  
  Database Models &amp;amp; Repositories
&lt;/h3&gt;

&lt;p&gt;Classes that interact with the database like Eloquent models and repository interfaces are perfect for contract testing. Validate expected behaviors for fetching, updating and relating data without depending on the backend.&lt;/p&gt;

&lt;h3&gt;
  
  
  API Controllers
&lt;/h3&gt;

&lt;p&gt;API surfaces define your application's public contract with external consumers. Test controller methods adhere to formats, require expected parameters, and return anticipated payloads and status codes. &lt;/p&gt;

&lt;h3&gt;
  
  
  Mailables &amp;amp; Notifications
&lt;/h3&gt;

&lt;p&gt;Notifications and mailables send critical communications. Contract test these by asserting views and data are rendered properly without transport concerns muddying tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Core Application Services
&lt;/h3&gt;

&lt;p&gt;Services encapsulate much of your application's business logic. Test service contracts return expected payloads and exceptions for a variety of input scenarios. This validates coreworkflows independently of UI logic.&lt;/p&gt;

&lt;p&gt;By focusing tests on these common artifacts, you can have confidence in critical contracts even as code evolves. Tests act as both validation and living documentation of intentions and boundaries for key classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Maintenance and Continuous Testing
&lt;/h2&gt;

&lt;p&gt;Contract tests provide ongoing value beyond initial development:&lt;/p&gt;

&lt;h3&gt;
  
  
  Refactoring Without Breaking Contracts
&lt;/h3&gt;

&lt;p&gt;Since tests focus on public APIs, internal refactors and optimizations won't cause failures if they don't change public behavior specifications. This allows safe changes over time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Versioning/Breaking Changes Clearly
&lt;/h3&gt;

&lt;p&gt;Before introducing breaking changes, update contracts by removing/modifying methods or arguments. This signals tests and consumers explicitly to change accordingly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integrating with Continuous Integration
&lt;/h3&gt;

&lt;p&gt;Add PHPUnit runs to your CI/CD pipeline. Now contract tests prevent regressions from being deployed automatically. Team members receive immediate feedback from failures.&lt;/p&gt;

&lt;p&gt;As codebases evolve rapidly, contracts catch where internal changes alter intended external behaviors. They document restrictions that maintain backwards compatibility over versions.&lt;/p&gt;

&lt;p&gt;With contracts, releasing with confidence is attainable. Refactors, features and optimizations land safely while preserving established public interfaces. Tests act as living documentation reflecting how code works and is meant to be used.&lt;/p&gt;

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

&lt;p&gt;Through every phase of development, from initial specs to ongoing maintenance, quality must remain the highest priority. Contract tests provide the structure and assurance that promises to users are kept, even as circumstances change.&lt;/p&gt;

&lt;p&gt;By clarifying expectations instead of implementation, contracts make code understandable from any perspective. They facilitate safe evolution, ensuring the present works as before while allowing imagination for what's next.&lt;/p&gt;

&lt;p&gt;Most of all, contract testing transforms relationships. Where fear of breakage once restricted progress, shared expectations built on honesty and respect empower risk-taking. Developers and customers walk together toward a same destination, bold yet careful in each step.&lt;/p&gt;

&lt;p&gt;This partnership of clarity and care is what raises a simple application to the level of trusted companion. When reliability is guaranteed, so too is the freedom to learn, grow and achieve ever more together. That is the true promise of contract testing in Laravel and beyond.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>testing</category>
      <category>bugs</category>
    </item>
    <item>
      <title>Rules to Reuse Laravel Code Like a Pro</title>
      <dc:creator>Isra Skyler</dc:creator>
      <pubDate>Thu, 02 Nov 2023 15:25:00 +0000</pubDate>
      <link>https://forem.com/isra_skyler/rules-to-reuse-laravel-code-like-a-pro-34g4</link>
      <guid>https://forem.com/isra_skyler/rules-to-reuse-laravel-code-like-a-pro-34g4</guid>
      <description>&lt;p&gt;Hey everyone, Isra here! I'm excited to share my tips for making reusable code.&lt;/p&gt;

&lt;p&gt;When I first started coding, I would write big messy programs that I couldn't change later. Yuck! Then I learned to break my code into smaller pieces that could be used over and over. &lt;/p&gt;

&lt;p&gt;The other day, a client wanted help with their website. After using some reusable code, they finished way faster! They were so happy that they said, We need to &lt;a href="https://hybridwebagency.com/seattle-wa/hire-laravel-developers/"&gt;hire Laravel Developers in Seattle&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now I want to teach you how to make your own reusable code. There are easy ways to share functions and classes between programs. We'll look at traits, interfaces and packages too. &lt;/p&gt;

&lt;p&gt;Turning your code into building blocks is fun. Other people can use the blocks in their programs too! Who knows, maybe clients will want to hire you in Seattle after seeing your reusable code skills.&lt;/p&gt;

&lt;p&gt;Let's get started! I'll show you my best techniques. By the end, you'll be an expert at reusable code like me. I'm excited for you to start sharing your code and helping more people.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extract Logic into Reusable Classes
&lt;/h2&gt;

&lt;p&gt;Let's talk about taking parts of your code and putting them into classes! This is so useful when you find yourself writing the same things over and over.&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples of common reusable classes
&lt;/h3&gt;

&lt;p&gt;For example, say you always validate user data the same way. You could make a UserValidator class with methods like validateName().&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserValidator&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;validateName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// validation logic&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;Another idea is a Mapper class that changes data from one format to another.&lt;/p&gt;

&lt;h3&gt;
  
  
  Guidelines for designing reusable classes
&lt;/h3&gt;

&lt;p&gt;When making reusable classes, keep them focused on one thing. The validator class is just for validation. &lt;/p&gt;

&lt;p&gt;Also make sure the classes don't rely on things outside itself. The validator shouldn't care where the data comes from.&lt;/p&gt;

&lt;p&gt;Try to plan for how others may use your class too. Add descriptive names and comments to your code.&lt;/p&gt;

&lt;p&gt;Reusable classes help DRY up your code (Don't Repeat Yourself). Just include the class and call the methods you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Traits for Common Behaviors
&lt;/h2&gt;

&lt;p&gt;Traits are really handy for putting together bits of code that you want to include in different classes. &lt;/p&gt;

&lt;h3&gt;
  
  
  Examples of reusable traits
&lt;/h3&gt;

&lt;p&gt;A common trait is SoftDeletes, which adds "deleted_at" and scopes to check for deleted objects.&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="kd"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;SoftDeletes&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;scopeWithTrashed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;withTrashed&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// other deletion methods&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating well-designed traits
&lt;/h3&gt;

&lt;p&gt;When making traits, keep them focused on one behavior. SoftDeletes should only deal with deleting/restoring.&lt;/p&gt;

&lt;p&gt;Avoid hard dependencies and external data in your traits. They should work anywhere they are used. &lt;/p&gt;

&lt;p&gt;Add documentation so others understand the trait's purpose and how to use it correctly.&lt;/p&gt;

&lt;p&gt;Traits are great for modeling cross-cutting concerns like timestamps, slugs, or other repetitive tasks. Keep your classes light by moving shared logic into reusable traits!&lt;/p&gt;

&lt;h2&gt;
  
  
  Bundle Logic into Packages
&lt;/h2&gt;

&lt;p&gt;When you have a lot of reusable code, it's time to package it up!&lt;/p&gt;

&lt;h3&gt;
  
  
  When to create packages
&lt;/h3&gt;

&lt;p&gt;If you find yourself including the same files in multiple projects, that's a sign it's package material. Anything others could download and use too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Folder structure and namespaces
&lt;/h3&gt;

&lt;p&gt;Packages generally follow a simple folder structure and namespaces so they are easy to use. Having a good readme helps explain how to install and use it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using existing packages as references
&lt;/h3&gt;

&lt;p&gt;Check out popular open source packages like &lt;a href="https://laravel.com/docs/10.x/packages"&gt;Laravel packages&lt;/a&gt; or others on Packagist. You can see examples of code organization, documentation and distribution that help users adopt it smoothly.&lt;/p&gt;

&lt;p&gt;Packaging your code keeps it organized and lets you share that usefulness with the whole community. Others can simply install it with Composer and get your reusable magic!&lt;/p&gt;

&lt;h2&gt;
  
  
  Leverage Laravel Components
&lt;/h2&gt;

&lt;p&gt;Laravel already gives us amazing tools to build on!&lt;/p&gt;

&lt;h3&gt;
  
  
  Core Laravel components
&lt;/h3&gt;

&lt;p&gt;Some major ones are Eloquent for databases, Collections for arrays, and Blades for views. These were made to be extended. &lt;/p&gt;

&lt;h3&gt;
  
  
  Building on core components
&lt;/h3&gt;

&lt;p&gt;For example, you could build a JSON API by adding methods to the Eloquent Model class. Or extend Blade with custom directives for your templates.&lt;/p&gt;

&lt;p&gt;Collections are so useful - you'll find yourself wanting to add reusable methods there too.&lt;/p&gt;

&lt;p&gt;The framework does the heavy lifting so focus on your custom logic. Leverage the solid foundations Laravel provides to easily create reusable solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Distribute with Composer
&lt;/h2&gt;

&lt;p&gt;Once your package is ready, Composer makes it easy to share!&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction to Composer
&lt;/h3&gt;

&lt;p&gt;Composer manages PHP dependencies. To use a package, you just add it to your composer.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"require"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"vendor/package"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^1.0"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Publishing packages to Packagist
&lt;/h3&gt;

&lt;p&gt;Upload your package to &lt;a href="https://packagist.org"&gt;Packagist&lt;/a&gt; so anyone can find it with a search. &lt;/p&gt;

&lt;h3&gt;
  
  
  Including packages in projects
&lt;/h3&gt;

&lt;p&gt;From the project folder, run &lt;code&gt;composer install&lt;/code&gt; to download. Then in PHP use:&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;require_once&lt;/span&gt; &lt;span class="s1"&gt;'vendor/autoload.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vendor\Package\Class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Composer, your packages can be used by anyone with a single command. Distribute that reusable goodness far and wide!&lt;/p&gt;

&lt;p&gt;Here are the interfaces sections with code examples:&lt;/p&gt;

&lt;h2&gt;
  
  
  Standardize Contracts with Interfaces
&lt;/h2&gt;

&lt;p&gt;Interfaces provide a common contract for classes to follow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of interfaces
&lt;/h3&gt;

&lt;p&gt;They allow classes to be interchangeable if they follow the same interface. Great for testing too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common interface examples
&lt;/h3&gt;

&lt;p&gt;A CrudInterface could mandate find/create/update methods:&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="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CrudInterface&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;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$id&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;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$data&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;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$data&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;A ModelInterface may expect properties like $table and relations:&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="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ModelInterface&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;getTable&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;relations&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;Interfaces ensure classes work the same way even if internal logic varies. They let code depend on standardized behaviors.&lt;/p&gt;

&lt;p&gt;Contracts promote code consistency, testability and reuse across applications. Define expected class APIs with interfaces!&lt;/p&gt;

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

&lt;p&gt;Writing reusable code has really improved my craft as a developer. It used to be that I would just build one-off scripts and functions without much thought for how others could leverage my work. But since learning techniques like extracting logic, using interfaces and publishing packages, I've found so much more satisfaction in my code.&lt;/p&gt;

&lt;p&gt;Nothing fills me with more pride than hearing how a package I created helped someone else complete a project faster or build something really cool. And it's rewarding to maintain reusable code over time, adding new features and keeping it useful for an ever-growing audience. More than that, focusing on reusability has made me a better problem solver. I understand code organization and design patterns at a deeper level now.&lt;/p&gt;

&lt;p&gt;Most of all, producing reusable code that spreads value to others has uncovered for me the true joy of this profession. While the work itself can be challenging at times, remembering why we do it makes all the difference. I'm thrilled to be part of uplifting the developer community in this small way, and I hope sharing these lessons proves equally helpful for your journey too.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>code</category>
    </item>
    <item>
      <title>Buttery-Smooth infinite scrolling with Laravel</title>
      <dc:creator>Isra Skyler</dc:creator>
      <pubDate>Thu, 02 Nov 2023 14:51:37 +0000</pubDate>
      <link>https://forem.com/isra_skyler/buttery-smooth-infinite-scrolling-with-laravel-10ee</link>
      <guid>https://forem.com/isra_skyler/buttery-smooth-infinite-scrolling-with-laravel-10ee</guid>
      <description>&lt;p&gt;As an experienced Laravel developer, I've been on a quest to find the perfect solution for smooth infinite scrolling. Many approaches failed under heavy usage until I successfully optimized a client's website. They needed thousands of products to stream continuously for their shoppers. Once I implemented my technique, even our most heavily populated categories breezed by effortlessly. The client was ecstatic and immediately decided to &lt;a href="https://hybridwebagency.com/bellevue-wa/hire-laravel-developers/"&gt;hire Laravel Developers in Bellevue&lt;/a&gt; to expand their flagship store. And now, I'm eager to share the secrets that led to their incredible success. In this piece, I'll guide you through crafting lightning-fast infinite scrolling, meeting the needs of even the most scroll-hungry users. Picture this: improved performance means happier customers and fewer headaches. Doesn't that sound appealing?&lt;/p&gt;

&lt;h2&gt;
  
  
  Laying the Groundwork
&lt;/h2&gt;

&lt;p&gt;For this project, I opted for the latest &lt;a href="https://laravel.com/"&gt;Laravel 8&lt;/a&gt; release for its blade components and improved routing. I also utilized Bootstrap for basic styling, jQuery for simplified DOM manipulation, and the Infinite Scroll library for smooth loading.&lt;/p&gt;

&lt;h3&gt;
  
  
  Folder Structure
&lt;/h3&gt;

&lt;p&gt;My file organization mirrored Laravel's default structure with a few custom tweaks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;app/&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Models&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Http/Controllers&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;database/&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;migrations&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;seeds&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resources/&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;js/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;css/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;views/&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up the Database
&lt;/h2&gt;

&lt;p&gt;The products were fetched from a &lt;a href="https://www.sqlite.org/index.html"&gt;SQLite database&lt;/a&gt;. I created a 'products' table with fields like id, title, and image using migrations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Designing the Database
&lt;/h3&gt;

&lt;p&gt;For this demo, I designed a simple 'products' database with only three fields: id, title, and image. The id is an auto-incrementing primary key, while title and image are text fields.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the Models
&lt;/h3&gt;

&lt;p&gt;Eloquent models served as the object-oriented interface to interact with the database. I generated a 'Product' model corresponding to the products table:&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Product&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="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$fillable&lt;/span&gt; &lt;span class="o"&gt;=&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;'image'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'products'&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;With the models and schema ready, the next step was to build a RESTful API to serve the product data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the API
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Building the Pagination Controller
&lt;/h3&gt;

&lt;p&gt;I generated an API controller to handle requests for paginated products data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan make:controller Api/ProductController
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Writing the API Endpoint
&lt;/h3&gt;

&lt;p&gt;The controller method accepted a page parameter to determine the data slice:&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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$page&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$perPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;paginate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$perPage&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;response&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;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$products&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;Testing with Postman confirmed the correct pagination of large product result sets to support eventual infinite scrolling of records.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Infinite Scroll
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Adding the Infinite Scroll Plugin
&lt;/h3&gt;

&lt;p&gt;I installed the jQuery Infinite Scroll plugin through NPM and added it to my main JS file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring Plugin Settings
&lt;/h3&gt;

&lt;p&gt;The plugin required specification of the container to monitor for scrolling and where to insert new content.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calling the API on Scroll
&lt;/h3&gt;

&lt;p&gt;Each scroll triggered a new request to fetch the subsequent page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Displaying Loaded Products
&lt;/h3&gt;

&lt;p&gt;The response data was used to build and insert new product elements into the container, activating the infinite scroll.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Enhancements
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Handling Loading State
&lt;/h3&gt;

&lt;p&gt;I added a loading indicator to replace products during data loading.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automatic vs. Manual Scrolling
&lt;/h3&gt;

&lt;p&gt;The plugin supported both behaviors; I chose manual scrolling for better user control.&lt;/p&gt;

&lt;h3&gt;
  
  
  Filtering Search Results
&lt;/h3&gt;

&lt;p&gt;A search box allowed users to dynamically filter results using query parameters that modified the dataset.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimizing Performance
&lt;/h2&gt;

&lt;p&gt;To prevent duplicate requests, I checked for ongoing loading by the user before subsequent scrolls. Additionally, response data was cached in memory for identical future queries, resulting in a seamless infinite loading experience for catalogs with thousands of products.&lt;/p&gt;

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

&lt;p&gt;The journey to create performant infinite scroll in Laravel was an incredible learning experience. Breaking down the problem into incremental steps made the solution surprisingly straightforward. What initially seemed a frustration with a sluggish user experience transformed into a fascinating technical challenge. My hope is that you now feel empowered to apply these techniques to your own Laravel projects. Beyond the code, the most fulfilling part was witnessing how a small optimization could significantly enhance the end user experience. Undoubtedly, there's always room for improvement. As site traffic and data volumes grow, more advanced caching, database patterns, and front-end logic might become necessary. However, this approach serves as an excellent starting point before delving into complex solutions. Remember, when endless scrolling appears impossible, with a touch of ingenuity, Laravel makes it surprisingly achievable. Keep learning, keep building, and relish the satisfaction of well-crafted projects. May your future development ventures be as rewarding as this one was for me.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
    </item>
    <item>
      <title>The Rebel Language That Rules the Web</title>
      <dc:creator>Isra Skyler</dc:creator>
      <pubDate>Mon, 25 Sep 2023 14:10:08 +0000</pubDate>
      <link>https://forem.com/isra_skyler/php-the-rebel-language-that-rules-the-web-lc2</link>
      <guid>https://forem.com/isra_skyler/php-the-rebel-language-that-rules-the-web-lc2</guid>
      <description>&lt;p&gt;So, picture this: I used to be that coder who raised an eyebrow at PHP. All those Silicon Valley tech enthusiasts and YouTube stars couldn't stop singing praises for the 'cooler' languages like Python and JavaScript. They made it sound like PHP was just a relic of the past. But then, I stepped into the real world of coding, working on projects for actual businesses, and guess what? The stats told a different story – PHP was everywhere, especially in the realm of small businesses. I was starting to work at &lt;a href="https://hybridwebagency.com/"&gt;Hybrid&lt;/a&gt; and everyday, the most work we had was on PHP.&lt;/p&gt;

&lt;p&gt;Then I thought let’s be a rebel, just like PHP. When everyone says that PHP is dead, let's talk about why it is still hanging on. What you read was not a clickbait but a serious conversation pending for a long time. Let's spill all the beans on why that is so, even when the cool kids look the other way. Plus, I'll share my secret sauce for choosing the right language for your projects – it's all about facts, not fads. Brace yourself, it's bound to be an eye-opener! 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  What Makes PHP A Juggernaut?
&lt;/h2&gt;

&lt;p&gt;In 2023, PHP commands a whopping &lt;strong&gt;78%&lt;/strong&gt; of websites globally. Yes, you read that right – nearly 4 out of 5 websites rely on PHP. And when it comes to content management systems, WordPress, built in PHP, reigns supreme with a staggering 60% market share.&lt;/p&gt;

&lt;p&gt;But why does PHP persist? Well, its user-friendly nature is a big reason. For newcomers and small businesses entering the web game, PHP offers a gentle learning curve – no complex frameworks or dependencies needed. Just find a server that supports it, upload your PHP files, and you're good to go.&lt;/p&gt;

&lt;p&gt;PHP also found its footing during the web's infancy, catering to startups hungry for rapid, cost-effective development. While other languages focused on the big league enterprise, PHP democratized web building for the underdogs.&lt;/p&gt;

&lt;p&gt;Once entrenched, technology is hard to dislodge. Millions of PHP sites stand because migration would be colossal. With such a vast PHP ecosystem, it's a pragmatic choice for many.&lt;/p&gt;

&lt;p&gt;Modern PHP (versions 5.6, 7.x, and 8.x) is no slouch. It's secure, scalable, and enterprise-ready. PHP 8, the latest iteration, packs even more punch for complex systems.&lt;/p&gt;

&lt;p&gt;Sure, other languages have dazzled – Java, C#, Python, Ruby, JavaScript, Go – each with its niche. But PHP thrives in real-world scenarios, despite its elitist detractors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Such Hate?
&lt;/h2&gt;

&lt;p&gt;So, why does PHP bear the brunt of developer disdain, even as it powers a significant chunk of the web?&lt;/p&gt;

&lt;p&gt;Much of the flak stems from PHP's humble beginnings as a beginner-friendly language. Born in the '90s, its simplicity aimed to welcome newcomers to web development. However, this led to it being labeled as an "amateur" or "toy" language, a favorite target for prestigious devs.&lt;/p&gt;

&lt;p&gt;Compare PHP's accessibility to languages like Java, C#, and Python, designed for enterprise-level complexity. The latter seemed like the choice of the pros. PHP bashers used it as a badge of superiority – &lt;strong&gt;"I'd never touch PHP."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PHP's inconsistent syntax didn't help its case, with quirks that became fodder for ridicule.&lt;/p&gt;

&lt;p&gt;Hipsters reveled in hating on PHP as overhyped and mainstream. It became the cool, rebellious stance among coders to scorn it.&lt;/p&gt;

&lt;p&gt;But behind the scenes, PHP quietly evolved. Object-oriented features in PHP 5, the rise of frameworks like Laravel, performance boosts in PHP 7, and enterprise-ready PHP 8 painted a different picture.&lt;/p&gt;

&lt;p&gt;Yet, first impressions linger. PHP's early reputation as unsophisticated stuck with purists, more focused on theory than real-world results.&lt;/p&gt;

&lt;p&gt;In reality, no language is universally "better" or "worse." Each has its place. But developers can be opinionated. PHP's accessible roots made it an easy target for snobbery.&lt;/p&gt;

&lt;p&gt;Then there is another factor, Boomers. All the boomers who are developers since ancient times or know a bit about development just only want PHP. Like our company provides &lt;a href="https://hybridwebagency.com/seattle-wa/custom-php-development-services/"&gt;PHP development Services in Seattle&lt;/a&gt; and the most of our clients who want us to work with them on Laravel or PHP are boomers. So this association also gives a hint that it is a relic of past.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Language Should You Choose?
&lt;/h2&gt;

&lt;p&gt;So when it comes to choosing languages for your own projects, how do you cut through the hype and technical elitism to make the best choice?&lt;/p&gt;

&lt;p&gt;First, take a hard look at your specific goals, target audience, timeline, budget and business needs. Are you a small startup trying to build an MVP as quickly and cheaply as possible? Or an established enterprise with long term scalability concerns? Identify your constraints.&lt;/p&gt;

&lt;p&gt;Then, examine the ecosystem surrounding each language. For example, while Ruby on Rails may be elegant, there's a smaller pool of developers skilled in it which could make hiring difficult. Meanwhile, Java has a massive ecosystem but also more ceremony around frameworks like Spring.&lt;/p&gt;

&lt;p&gt;Look critically at the dependencies and tooling too. For instance, JavaScript's npm was notoriously fragile, causing "dependency hell" when packages broke. Languages with more oversight like C# avoid this issue.&lt;/p&gt;

&lt;p&gt;Also consider hosting and deployment factors. PHP is easy to host anywhere. But deploying, say, a complex Dockerized microservices app requires more orchestration.&lt;/p&gt;

&lt;p&gt;Legacy technology plays a role too. Rewriting an older PHP system into a trendy new stack may not be worth the resources. Enhancing it incrementally could be smarter.&lt;/p&gt;

&lt;p&gt;And don't overlook language familiarity in your own team. Leveraging existing skills boosts productivity dramatically. A less popular language may have a steeper learning curve.&lt;/p&gt;

&lt;p&gt;Beyond that, analyze usage statistics in your industry rather than broad hype. While Python may be very popular overall, is PHP or .NET more common for the types of applications you build? Let real-world data guide you.&lt;/p&gt;

&lt;p&gt;Most importantly, don't let peer pressure sway you. Developers can become religious about their preferred languages for emotional rather than practical reasons. Make a rational choice based on evidence.&lt;/p&gt;

&lt;p&gt;Every language brings distinct advantages that make it suitable for certain use cases. The "best" choice depends on context. For example, while I love Python's elegance, for web-based business apps I often turn to PHP or JavaScript for their ubiquity.&lt;/p&gt;

&lt;p&gt;Every technology has downsides too. But used strategically, even imperfect tools can build something incredible. Focus on the business problem first, then choose the language that pragmatically makes the most sense.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't Believe the Hype - Examine the Evidence
&lt;/h2&gt;

&lt;p&gt;So what are the key lessons we can learn from taking an honest look at PHP's ongoing prominence compared to the hype around trendier languages?&lt;/p&gt;

&lt;h3&gt;
  
  
  Focus on statistics, not opinions.
&lt;/h3&gt;

&lt;p&gt;Programming language discourse online can get extremely inflammatory and tribal. But rather than getting sucked into the zealotry, stay objective. Look at hard data on usage and job demand to see the full picture. The numbers don't lie.&lt;/p&gt;

&lt;h3&gt;
  
  
  Evaluate tools in context.
&lt;/h3&gt;

&lt;p&gt;No language is the unequivocal "best" or "worst" universally. Each has pros and cons that make it suitable for different purposes. Make choices based on your specific constraints and use cases, not abstraction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Right tool for the job.
&lt;/h3&gt;

&lt;p&gt;Given the same task, an artisan woodworker and a mechanical engineer would use very different tools. Similarly, different languages excel at certain jobs. Select what pragmatically makes the most sense.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't reinvent the wheel.
&lt;/h3&gt;

&lt;p&gt;Before discarding legacy systems to jump on trends, consider incremental improvement. A bird in the hand is worth two in the bush. Refactoring working code into new tech may not be worth it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Beware the hype cycle.
&lt;/h3&gt;

&lt;p&gt;Developers chase after the hot new thing constantly. But early-stage languages lack maturity and stability. Their pitfalls aren't yet known. Proven languages have already dealt with growth pains.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stay flexible.
&lt;/h3&gt;

&lt;p&gt;Languages come and go. Frameworks rise and fall. Sticking dogmatically to just one narrow skillset leaves you vulnerable. Maintain breadth across languages and embrace learning.&lt;br&gt;
Watch for elitism. Some developers disparage certain languages irrationally to sound sophisticated. But use by beginners doesn't make a language inherently bad. Judge tools by their utility alone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Focus on transferable skills.
&lt;/h3&gt;

&lt;p&gt;While syntax differs, core concepts like logic and problem-solving translate across languages. Invest in honing those rather than fixating on one language.&lt;/p&gt;

&lt;p&gt;At the end of the day, no one language or technology will ever dominate all use cases. As developers, we have an incredible array of tools at our fingertips, each with strengths suited for particular applications. Stay open-minded, focus on business value, and let real data guide your technology choices. The hype will fade, but good code endures. Peace! ✌&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;PHP usage statistics: &lt;a href="https://w3techs.com/technologies/details/pl-php"&gt;https://w3techs.com/technologies/details/pl-php&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;WordPress market share: &lt;a href="https://w3techs.com/technologies/details/cm-wordpress"&gt;https://w3techs.com/technologies/details/cm-wordpress&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PHP versions: &lt;a href="https://www.php.net/releases/"&gt;https://www.php.net/releases/&lt;/a&gt;
Latest PHP 8.2 features: &lt;a href="https://www.php.net/releases/8.2/en.php"&gt;https://www.php.net/releases/8.2/en.php&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Laravel framework: &lt;a href="https://laravel.com"&gt;https://laravel.com&lt;/a&gt;
npm dependency issues: &lt;a href="https://npm.github.io/how-npm-works-docs/theory-and-design/dependency-hell.html"&gt;https://npm.github.io/how-npm-works-docs/theory-and-design/dependency-hell.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;JavaScript frameworks: &lt;a href="https://expressjs.com"&gt;https://expressjs.com&lt;/a&gt;, &lt;a href="https://angular.io"&gt;https://angular.io&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Web development basics: &lt;a href="https://www.w3schools.com/"&gt;https://www.w3schools.com/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>beginners</category>
      <category>laravel</category>
    </item>
  </channel>
</rss>
