<?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: Sam Ringleman</title>
    <description>The latest articles on Forem by Sam Ringleman (@theringleman).</description>
    <link>https://forem.com/theringleman</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%2F110615%2Fcf1abeb6-a4e8-4b59-a47a-ae10974e5388.jpeg</url>
      <title>Forem: Sam Ringleman</title>
      <link>https://forem.com/theringleman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/theringleman"/>
    <language>en</language>
    <item>
      <title>10 Quick Tips that Will Speed Up Your Development</title>
      <dc:creator>Sam Ringleman</dc:creator>
      <pubDate>Mon, 08 Jul 2019 14:33:30 +0000</pubDate>
      <link>https://forem.com/theringleman/10-quick-tips-that-will-speed-up-your-development-2p2b</link>
      <guid>https://forem.com/theringleman/10-quick-tips-that-will-speed-up-your-development-2p2b</guid>
      <description>&lt;p&gt;Everyone wants the silver bullet. How do I learn x in 24 hours? Well, I hate to be the bearer of bad news, but coding is hard. You will want to bang your head against the desk, you will want to cry, you will cry. But in time it gets better. And there are ways to speed up learning. Here are 10 quick tips that you can implement right away, not only will it speed up your learning, but it will speed up your development process.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Use a debugger.
&lt;/h3&gt;

&lt;p&gt;Some consider this knowledge a basic rule for &lt;a href="https://softwareengineering.stackexchange.com/a/78154"&gt;coding&lt;/a&gt;. Just using this tool alone will help you gain a wonderful knowledge of your code. It is not enough to use print statements. You must get comfortable with the execution of your code. That is how you learn. Here is a &lt;a href="https://codejourney.io/comfortable-debugger"&gt;tutorial&lt;/a&gt; on the basic usage of a debugger.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;While simple to learn, I believe that debugging should be included in every university curriculum and practiced at every software development company. – &lt;a href="https://www.linkedin.com/in/peterjfranklin/"&gt;Peter Franklin&lt;/a&gt; CEO Pryze&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  9. Be expressive.
&lt;/h3&gt;

&lt;p&gt;In a code review, I love seeing &lt;code&gt;theArrayReturnedFromAnIterator&lt;/code&gt; over &lt;code&gt;i&lt;/code&gt;. In fact, I won’t approve a review if I see variables like this. Being expressive provides a quick glimpse into your code. Not only for others but for yourself 6 months down the road. What does the variable i mean in this context? Is it an iterator? Is it the count in a for loop? Is i an object (I don’t name all my variables based on what type it is, that what annotations are for)? Be more explicit with your variables. It may seem tedious to write it out now, but you are paying yourself dividends in the long run. Sometimes, those Java devs have the right idea.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z5Lqeoqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.codejourney.io/2019/07/03203046/nesa-by-makers-IgUR1iX0mqM-unsplash-1-1024x683.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z5Lqeoqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.codejourney.io/2019/07/03203046/nesa-by-makers-IgUR1iX0mqM-unsplash-1-1024x683.jpg" alt="A group working together on code."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Communicate with your team.
&lt;/h3&gt;

&lt;p&gt;Always communicate. Express your ideas, no matter how new you are to the code. If you come from a different job, please, express your experience. No, we don’t think you are bragging. You could save the company millions, and your team countless hours. You know more than you think. Feel free to share it. In fact, feel obligated to share it.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Use an IDE.
&lt;/h3&gt;

&lt;p&gt;If you let them, your tools will make you a better programmer. Quality in tools is important. The latest text editor is fine, but an IDE will allow you to dive deep into your code. There are free options that are close to more professional routes. VSCode has become the standard for a reason, first off it kicks ass. Second, with a couple simple plugins you can emulate a lot of the features that more expensive IDE’s provide. It will only make refactoring and debugging easier. When you are ready, buy yourself a license to JetBrains. Their tools have taught me more about programming than I can express.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Follow coding standards.
&lt;/h3&gt;

&lt;p&gt;Standards are there for a reason. You may not like 4 spaces, or 2, or tabs, or I don’t care. What is the language standard? I want to jump in your PHP class and know that all your properties are declared at the top. That your static methods are below your properties. I want to look at an if statement and be able to jump from block to block. You can automate this using a linter. A linter will keep you up to date with the standards that the community has set. In time, you will follow this blindly. Use it. Love it. Automate it.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Keep methods small.
&lt;/h3&gt;

&lt;p&gt;Hey, see that 650 line controller action? That’s how bugs are born.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In most cases, excessively long methods are the root of all evil. – &lt;a href="https://sourcemaking.com"&gt;Source Making&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you have a method that has nested conditionals, break that up. Make 15 small methods. You can hop to each method and know that it does one thing. Since you also followed step 9, you will be able to tell exactly what that method does because of the name of the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//This is hard to read, and gets messy quick.&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$date&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;before&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;SUMMER_START&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;$date&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;after&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;SUMMER_END&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;$charge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;$winterRate&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;$winterServiceCharge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;$charge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$quantity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;$summerRate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;



&lt;span class="c1"&gt;//This is much better.&lt;/span&gt;
&lt;span class="c1"&gt;//Thank you Source Making for the wonderful examples.&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isSummer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$date&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;$charge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;summerCharge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;$charge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;winterCharge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$quantity&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;Trust me, be more explicit, it will make you much faster in the long run.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Keep classes single use.
&lt;/h3&gt;

&lt;p&gt;Create new classes, no you are not bloating the code! I suffered from this phobia for far too long. In most modern tools, jumping between classes is a simple keystroke. In PHPStorm for instance, hover over a class name, hit cmd + b.Jumps you right to the class. Like the last tip, I want to read your class name/namespace and know off the bat what this class does. Make a new class with only one or two methods. You will extend it in time, or you will learn what can extend from it. Small is always easier to test, and easier to read. Take a closer look at the &lt;a href="https://en.wikipedia.org/wiki/SOLID"&gt;SOLID&lt;/a&gt; principles if you want a more in-depth explanation.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Break things.
&lt;/h3&gt;

&lt;p&gt;Version control is there for a damn good reason. Make that new branch. Break the shit out of everything! Test your idea.See where it succeeds, and see where it fails. This is how you learn. When I was first hired, I was horrified to try anything different than my seniors. I thought that my code needed to be like theirs. When I finally became confident that, yes I was going to break things, and that it was alright. I started to learn. That is one of the joys of software.You can take the internet out in one misplaced &lt;a href="https://www.theverge.com/2017/3/2/14792442/amazon-s3-outage-cause-typo-internet-server"&gt;semicolon&lt;/a&gt;. It’s alright though, it can always be reverted. After all, it’s just a website &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CDd67yc0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f609.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CDd67yc0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f609.png" alt="😉"&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Write tests unapologetically.
&lt;/h3&gt;

&lt;p&gt;If your company doesn’t test, be the change. Lead that charge. Test your code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You shouldn’t ask to write unit tests for the same reason you shouldn’t ask to do a good job. – &lt;a href="https://medium.com/@theobendixson/how-not-to-convince-your-boss-to-let-you-write-unit-tests-155bd970d7d1"&gt;Ted Bendixson&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is nothing more liberating than running your unit tests. It may take a little longer up front, but once that test coverage grows, you can refactor to your heart’s desire. You will know that your code works. And best of all, you can blame Chad over there, when he adds code and doesn’t run the testing suite…&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Build personal projects.
&lt;/h3&gt;

&lt;p&gt;This is the single most important thing you can do for yourself. Learn outside of work. Build that passion project that deep down you know is going to light you on fire. You will gain such a deep understanding of your language, your tools, your processes. It is invaluable. I cannot express in this short post how important this is. I implore you if you want to get good, build outside of work. Chase that passion. Then turn it into something someone else will use. Share it, live it, love it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Closing
&lt;/h3&gt;

&lt;p&gt;Some of these steps will take you time to learn, unit testing. Others you can implement immediately, using a debugger.The point is, if you use these steps, you will get faster at developing. Let me know in the comments what you think and if you have any other tips that have helped you along the way!&lt;/p&gt;

&lt;p&gt;If you would like to join me in along my &lt;a href="https://codejourney.io"&gt;codeJourney&lt;/a&gt;, please come &lt;a href="https://codejourney.io"&gt;visit&lt;/a&gt;, read a few posts and join in on the conversation. I would love to see and hear from more of you!&lt;/p&gt;

</description>
      <category>tips</category>
    </item>
    <item>
      <title>Stop Using Print and Die Statements</title>
      <dc:creator>Sam Ringleman</dc:creator>
      <pubDate>Mon, 24 Jun 2019 22:32:18 +0000</pubDate>
      <link>https://forem.com/theringleman/stop-using-print-and-die-statements-5g8p</link>
      <guid>https://forem.com/theringleman/stop-using-print-and-die-statements-5g8p</guid>
      <description>&lt;p&gt;Today we are going to go over debugging 101 tips for using print or die statements. Discussing what you should do &amp;amp; why. The tips presented here are suitable for devs of all skill levels. Simply follow the instructions below to get your code on.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Traditional Way
&lt;/h3&gt;

&lt;p&gt;The statements below exist in every language &amp;amp; we all rely on them to &lt;a href="https://codejourney.io/tag/debug" rel="noopener noreferrer"&gt;#debug&lt;/a&gt; at some point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ruby:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Message goes here"&lt;/span&gt;
&lt;span class="nb"&gt;exit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  PHP:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;print_f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Message goes here"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;die&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Javascript:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Message goes here&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;fail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  You Get the Point
&lt;/h3&gt;

&lt;p&gt;Instead of the above, try using purpose built debugging tools. Believe it or not, using tools &lt;em&gt;can&lt;/em&gt; make you a better programmer. A senior engineer once told me (early in my career) that anyone who doesn’t use a debugger is a chump. I could not agree more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Seeing the Entire Picture
&lt;/h3&gt;

&lt;p&gt;For those who are unfamiliar with debuggers, here is a quick introduction.&lt;/p&gt;

&lt;p&gt;Debuggers stop a program mid execution, show the state of it &amp;amp; allow you to take a deep dive in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.43.59-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.43.59-PM.png"&gt;&lt;/a&gt;A PHP script stopped mid execution using a debugger.&lt;/p&gt;

&lt;p&gt;A debugger also allows you to step through your code, so you can see what is happening live.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.44.09-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.44.09-PM.png" alt="Code being stepped through with debugger."&gt;&lt;/a&gt;A PHP script stepped down two lines in the same method.&lt;/p&gt;

&lt;p&gt;Without this tool, seeing the entire picture of a program is next to impossible (if I am wrong about that, please show me a different way).&lt;/p&gt;

&lt;p&gt;A debugger also allows you to check all current variable values at a specific point in execution. Globals, local variables, even variables buried deep in a private method. You can see it all.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.43.00-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.43.00-PM.png" alt="A list of variables shown in the debugger."&gt;&lt;/a&gt;A look at all variables in the current execution context.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.43.09-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.43.09-PM.png" alt="A variable object extended."&gt;&lt;/a&gt;An in depth look at the “$request” variable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Anyone who doesn’t use a debugger is a chump.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Debuggers also show you the call stack leading up to that point of execution. Some even let you jump around in the stack. It shows the file, and method that was executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.43.31-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2FScreen-Shot-2019-06-25-at-3.43.31-PM.png" alt="A call stack show in a debugger."&gt;&lt;/a&gt;A call stack showed by the debugger.&lt;/p&gt;

&lt;h3&gt;
  
  
  Engage with It
&lt;/h3&gt;

&lt;p&gt;Not only can you take a look, but you can manipulate as well. Change the value of anything and execute the program based on the values that you have chosen at that time. The power that comes with this can be wonderful. For instance, you want to assure that your data being fed into the method is correct, you can change what is incorrect just to test if your code does what you expect. This is a double edge sword though, the rest of the program executes on the data that you changed. Be careful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Gain a Deep Understanding
&lt;/h3&gt;

&lt;p&gt;By stepping into the code, you automatically make the source code of whatever framework/library you are working with relatable. You are able to visably see the code change your data. This helps gain a deep understanding of system control flow, especially when using a third party library or framework. I think one of the best ways to learn a framework is to dive into the source. A debugger forces that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2Fdive-in.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.codejourney.io%2Fwp-content%2Fuploads%2Fdive-in.jpg" alt="Underwater turtle diving"&gt;&lt;/a&gt;&lt;br&gt;
Dive into the source&lt;/p&gt;

&lt;h3&gt;
  
  
  Up Next
&lt;/h3&gt;

&lt;p&gt;My next article in this series will be a closer look at the finer details of a &lt;a href="https://codejourney.io/tag/debugger" rel="noopener noreferrer"&gt;#debugger&lt;/a&gt;. So today I want to leave you with one action item that you can start to implement using Javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  debugger;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;someThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;er&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;howdy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ing&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 keyword in &lt;a href="https://codejourney.io/tag/javascript" rel="noopener noreferrer"&gt;#Javascript&lt;/a&gt; allows you to fire a debugger automatically when executed in Chrome or Firefox. Note that you must have your console open in order for the debugger to stop execution. Then you can step through and into methods as needed. No more console.log();&lt;/p&gt;

&lt;p&gt;Use it. Get comfortable with it. It works.&lt;/p&gt;

</description>
      <category>tutorials</category>
      <category>debug</category>
      <category>debugger</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Parents in tech, how do you balance your time between coding and playing with your kids?</title>
      <dc:creator>Sam Ringleman</dc:creator>
      <pubDate>Sun, 16 Jun 2019 19:32:15 +0000</pubDate>
      <link>https://forem.com/theringleman/parents-in-tech-how-do-you-balance-your-time-between-coding-and-playing-with-your-kids-6p6</link>
      <guid>https://forem.com/theringleman/parents-in-tech-how-do-you-balance-your-time-between-coding-and-playing-with-your-kids-6p6</guid>
      <description>&lt;p&gt;First off happy Father's Day to all the dad's out there in tech! As far as I am concerned, parenting is the hardest job on the face of the planet. As well as the most rewarding!&lt;/p&gt;

&lt;p&gt;I am highly passionate about coding and I have a side project in the works outside of my normal job. I try to spend as much time on it as my family deems fair.&lt;/p&gt;

&lt;p&gt;If you are anything like me, how do you balance your time between coding and your family? Do you pull late nights or super early mornings? Do you ever feel guilty about coding when your kids are around? Or do you just code at work and call it a day?&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>I used to be an engineer on the railroad, now I engineer software, Ask Me Anything!</title>
      <dc:creator>Sam Ringleman</dc:creator>
      <pubDate>Fri, 14 Jun 2019 21:59:23 +0000</pubDate>
      <link>https://forem.com/theringleman/i-used-to-be-an-engineer-on-the-railroad-now-i-engineer-software-ask-me-anything-3j4b</link>
      <guid>https://forem.com/theringleman/i-used-to-be-an-engineer-on-the-railroad-now-i-engineer-software-ask-me-anything-3j4b</guid>
      <description>&lt;p&gt;Hi! I am Sam. I have had the dream of being in tech since I was little, but different life events happened and I didn't pursue my dream until what I consider late. I tried multiple times to learn to code and failed, I even dropped out of college while I was enrolled for CS. &lt;/p&gt;

&lt;p&gt;I eventually went to work for BNSF (the big orange trains) as a conductor, and then became an engineer (the one who drives the train, not the same type of engineer). After 4 years, I got laid off. I decided while I was unemployed that it was time for a change. So I learned to code, and now three plus years later, I am an engineer with the R&amp;amp;D team at RE/MAX.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pbJZ0ObQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/kbm6hd3l92tknwedr3rm.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pbJZ0ObQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/kbm6hd3l92tknwedr3rm.jpeg" alt="Train air gauge, railroad spike, pocket watch, BNSF award, and three locomotive reversers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I keep these in the window above my desk at home. They are there to remind me of where I came from. To remind me that complacency is not ok. To remind me to keep pushing forward every day. I have a motto and it is cliche, but if I can do it, so can you.&lt;/p&gt;

&lt;p&gt;I want to be an advocate for others trying to break into our industry. I have been so blessed by this move, and I want nothing more than to see others reach the same goal. So please ask me anything, I would love to get to know you!&lt;/p&gt;

</description>
      <category>ama</category>
    </item>
    <item>
      <title>Quick API Debugging Tip</title>
      <dc:creator>Sam Ringleman</dc:creator>
      <pubDate>Fri, 14 Jun 2019 14:10:18 +0000</pubDate>
      <link>https://forem.com/theringleman/quick-api-debugging-tip-2m3i</link>
      <guid>https://forem.com/theringleman/quick-api-debugging-tip-2m3i</guid>
      <description>&lt;p&gt;I learned this trick a couple years ago and it has saved me so much time that it's only prudent to share. &lt;/p&gt;

&lt;p&gt;Say you built an API, and you are working on the front end to consume the awesomeness that is your API. You get your form set up to log your user in. Hit the send button and you get a 400 response. Wait, what the what?  I have a very simple starting point that you can use with tools you are probably already using.&lt;/p&gt;

&lt;p&gt;This tip involves either Google Chrome, or Mozilla Firefox. There might be more, but these are the two browsers that I use. As well as the &lt;a href="https://www.getpostman.com/"&gt;Postman&lt;/a&gt; app. These are all free to use, so use them. They are awesome!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your dev tools.

&lt;ul&gt;
&lt;li&gt;Chrome (cmd + alt + i)&lt;/li&gt;
&lt;li&gt;Firefox (cmd + alt + i)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Go to the network tab.

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hyM3_Wmd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/pw2VHnS.png" alt="Tabs of the chrome developer tools"&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Find the failing request.

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p5MTrZIf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/MCoFlJT.png" alt="Failed request in chrome developer tools"&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Right click and copy as cURL.

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WG9Bmr9w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/xCpP2kA.png" alt="Menu items for chrome with Copy as cURL highlighted"&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Open Postman and select import.

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G0sNIZNj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/z2VtUgW.png" alt="Postman header showing the import button"&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Select the "Paste Raw Text" tab.

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g6GMyXTE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/6S62HWG.png" alt="Postman modal showing the import tabs with Paste Raw Text highlighted"&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Paste your cURL request you copied from your browser and hit Import.

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e6cKRFWQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/hxYsPWf.png" alt="Postman modal with raw cURL request pasted in"&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Send your request for much easier debugging.

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c4cat9Pc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/TVGjb6s.png" alt="Postman after the request has been imported"&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One thing to take note of, your request is completely imported into Postman. This makes it very simple to debug your request. You can see your data easier and any headers sent in their nice UI.&lt;/p&gt;

&lt;p&gt;As a backend dev, I find it handy for the front end dev to send that request to me in chat. This way I can recreate what is breaking in my API with the exact data that they are sending.&lt;/p&gt;

&lt;p&gt;I use this trick on a daily basis. I hope that you will too!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>console</category>
      <category>postman</category>
      <category>api</category>
    </item>
    <item>
      <title>How Testing Speeds Up My Workflow</title>
      <dc:creator>Sam Ringleman</dc:creator>
      <pubDate>Sat, 25 May 2019 15:10:00 +0000</pubDate>
      <link>https://forem.com/theringleman/how-testing-speeds-up-my-workflow-2gm3</link>
      <guid>https://forem.com/theringleman/how-testing-speeds-up-my-workflow-2gm3</guid>
      <description>&lt;p&gt;So I am relatively new to the coding world. I have been a professional engineer now for a little over three years. Happily, I recently accepted a new position as a mid-level engineer, yay! I have learned quite a lot by taking a new position, one of the more prominent items to stand out in my mind is testing.&lt;/p&gt;

&lt;p&gt;At a previous job, we were on the mindset that testing would slow us down. There were some strong opponents of testing. The most prominent argument that I heard was that testing took too much time. There is the time investment in writing the tests. There is the time investment running the tests. There is the time investment maintaining the tests. While these arguments have a point, you do spend time setting everything up. I counter this by saying the time investment is minor. The benefits that you gain through testing far outweigh the time invested upfront. I have experienced this first hand through testing my model relationships in Laravel.&lt;/p&gt;

&lt;p&gt;My previous workflow for testing relationships was a bit awkward and clunky. I would do the database migration first, and in our situation, that was a straight SQL query to a testing db. Then go in the models and create the relationship (hasOne, hasMany, etc). Then to make sure the relationship is built right, I write a test controller. This controller has a test endpoint, oh and let's not forget a test route. The test controller action was simple enough, I don't need input from a user, but I do need a model ID. If that ID  exists in the database I jump over to the database find an id for each model and join them in the controller action... If there was no ID I create a model and  grab that ID. Then after I get my test controller action set up, I hop over into Postman fire off the request. Low and behold my test action borks. Hmm, I must have screwed something up in the models. Jump into bug tracking mode. I hop around the models, look at the db schema, back to routes file, and oh shoot, there it is, a mistake in my test action. Fix the small issue, then back into Postman, and yay it works. Now to finish hooking up the relationship in the code that matters. Then delete the test controller and routes and be on my merry way. This is a slow and clunky workflow, that is very error prone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp91faja8f32i6jp0on04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp91faja8f32i6jp0on04.png" alt="An annoyed Meowth" width="530" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fast forward to the new job, new lead. We do a lot of testing, unit, integration, feature, even UAT!&lt;/p&gt;

&lt;p&gt;My new workflow goes like this. I design my schema and write a migration. Then I write a test for the relationship I want between the models. I then go into the model, and code the relationship. And I run my test. That's it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftkf53yu5z2p2j8cawr27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftkf53yu5z2p2j8cawr27.png" alt="Looney Toons that's all folks" width="800" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty lame post huh? Nah, let's go through an example together. I am using Lumen in this example. All the code is here, so if you want to follow along, install a fresh Lumen project and let's build some guitars!&lt;/p&gt;

&lt;p&gt;Start off with the models schema and migrations. Run these two commands in your projects directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan create:migration create_brands_table
php artisan create:migration create_guitars_table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what our migrations are going to look like.&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;CreateBrandsTable&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Migration&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * Run the migrations.
     *
     * @return void
     */&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;up&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="o"&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;'brands'&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;Blueprint&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;bigIncrements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;timestamps&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="cd"&gt;/**
     * Reverse the migrations.
     *
     * @return void
     */&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;down&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dropIfExists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'brands'&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;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;CreateGuitarsTable&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Migration&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * Run the migrations.
     *
     * @return void
     */&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;up&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="o"&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;'guitars'&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;Blueprint&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;bigIncrements&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'type'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;unsignedBigInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'brand_id'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;foreign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'brand_id'&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;references&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&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;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'brands'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;timestamps&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="cd"&gt;/**
     * Reverse the migrations.
     *
     * @return void
     */&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;down&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dropIfExists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'guitars'&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;Two very simple models and the relationship that we are designing here is a one to many. A guitar has one brand, and a brand can have many guitars.&lt;/p&gt;

&lt;p&gt;Let's create our models now. Our &lt;code&gt;Brand&lt;/code&gt; model looks 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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Brand&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="cd"&gt;/**
     * @var string
     */&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;'brands'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * @var array
     */&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;'name'&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;And our &lt;code&gt;Guitar&lt;/code&gt; model looks 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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Guitar&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="cd"&gt;/**
     * @var string
     */&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;'guitars'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * @var array
     */&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;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'type'&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;Again, nothing complicated, but now we get into the fun part. I like to write my tests before I write out my logic. So let's get started by writing the test for the &lt;code&gt;Guitar&lt;/code&gt; to have one &lt;code&gt;Brand&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create the file &lt;code&gt;GuitarTest&lt;/code&gt; in your testing directory. And add the first test &lt;code&gt;it_should_have_one_brand&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GuitarTest&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="cd"&gt;/**
     * @test
     */&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;it_should_have_one_brand&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;We need to have a guitar instance to run our tests. When it comes to testing Eloquent models it makes sense to use them with the database. So let's create a factory for the &lt;code&gt;Guitar&lt;/code&gt; and &lt;code&gt;Brand&lt;/code&gt; models.&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;ModelFactory&lt;/code&gt; file insert 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="nv"&gt;$factory&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;App\Models\Guitar&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="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Faker&lt;/span&gt;&lt;span class="nc"&gt;\Generator&lt;/span&gt; &lt;span class="nv"&gt;$faker&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="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$faker&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'type'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$faker&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;randomElement&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'acoustic'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'electric'&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="nv"&gt;$factory&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;App\Models\Brand&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="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Faker&lt;/span&gt;&lt;span class="nc"&gt;\Generator&lt;/span&gt; &lt;span class="nv"&gt;$faker&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="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$faker&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&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;Great! Now we can start the testing! So jump back to the &lt;code&gt;GuitarTest&lt;/code&gt; that we started earlier and let's create our models.&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;it_should_have_one_brand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$guitar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Guitar&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="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="nv"&gt;$brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Brand&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="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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run your test here you are going to get an SQL error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1364 Field 'brand_id' doesn't have a default value (SQL: insert into `guitars` (`name`, `type`, `updated_at`, `created_at`) values (Jaydon Daugherty, acoustic, 2019-05-23 22:59:12, 2019-05-23 22:59:12))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uh oh, turns out that we cannot create a &lt;code&gt;Guitar&lt;/code&gt; at this point without first having a &lt;code&gt;Brand&lt;/code&gt;. That is good news, we know that our schema is working. There are a few options available to use here. We can implement a factory state which will create a &lt;code&gt;Brand&lt;/code&gt; for us. We could change the schema and make the foreign key nullable. Or we can not create the model and  instantiate it.&lt;/p&gt;

&lt;p&gt;Let's choose the latter and and leave our schema alone.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;create&lt;/code&gt; method on the factory creates your model and persists it to the database. The &lt;code&gt;make&lt;/code&gt; method instantiates it and fills it with data leaving you to do the persisting. The reason that we got the SQL error is because of the create method. At that point we were trying to save the model, without giving a value to the foreign key that we created. We also did not specify that it was nullable. It does not make sense to me to have a &lt;code&gt;Guitar&lt;/code&gt; without a &lt;code&gt;Brand&lt;/code&gt; in this process (homebuilders, I feel you). So we get an error without supplying a value.&lt;/p&gt;

&lt;p&gt;So what do we do? 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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;it_should_have_one_brand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$guitar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Guitar&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nv"&gt;$brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Brand&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="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="nv"&gt;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;brand&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;associate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$brand&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nv"&gt;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&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;Please note that we changed the &lt;code&gt;Guitar&lt;/code&gt; factory call from &lt;code&gt;create&lt;/code&gt; to &lt;code&gt;make&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now to finish the test, add an assertion to make sure that we have the correct data, and this is a good test!&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;it_should_have_one_brand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$guitar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Guitar&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nv"&gt;$brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Brand&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="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="nv"&gt;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;brand&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;associate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$brand&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nv"&gt;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&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;\App\Models\Brand&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;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;brand&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 run your test, you should get an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BadMethodCallException : Call to undefined method App\Models\Guitar::brand()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which makes perfect sense, we haven't implemented the relationship yet. Let's do that. Head back into the Guitar model and add:&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="cd"&gt;/**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */&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;brand&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;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;belongsTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Brand&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And run the test again. You should be all green now!&lt;/p&gt;

&lt;p&gt;We can go a little further with our assertion if we want to make sure that the guitar did save the same model. Note that this is optional, this test is fine as is.&lt;/p&gt;

&lt;p&gt;The completed test looks 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="cd"&gt;/**
     * @test
     */&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;it_should_have_one_brand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$guitar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Guitar&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nv"&gt;$brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Brand&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="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="nv"&gt;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;brand&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;associate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$brand&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nv"&gt;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&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;\App\Models\Brand&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;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;brand&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;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$brand&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$guitar&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;brand&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&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;Great so we have our relationships tested right? Wrong. We have one way of a two-way relationship tested. Let's hook up our Brand model to their many Guitar models.&lt;/p&gt;

&lt;p&gt;Create the BrandsTest in your testing directory and write the first test.&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;BrandsTest&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="cd"&gt;/**
     * @test
     */&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;it_should_have_many_guitars&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;Remember that we have a one to many relationship here, so a brand can have many guitars. And that is what we are going to test for. Let's make our models:&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;it_should_have_many_guitars&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Brand&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="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="nv"&gt;$guitars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Guitar&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="mi"&gt;5&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;make&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;Then we need to associate them and run our assertions against the relationship. So our final test is going to 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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;it_should_have_many_guitars&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Brand&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="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="nv"&gt;$guitars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Guitar&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="mi"&gt;5&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;make&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nv"&gt;$brand&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;guitars&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;saveMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$guitars&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;assertCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$brand&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;guitars&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;assertContainsOnlyInstancesOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;\App\Models\Guitar&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;$brand&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;guitars&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 when we run this test, like last time, we are going to get the error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BadMethodCallException : Call to undefined method App\Models\Brand::guitars()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, it makes perfect sense, we haven't implemented that logic in our model yet. Add the &lt;code&gt;guitars&lt;/code&gt; method to your &lt;code&gt;Brand&lt;/code&gt; 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="cd"&gt;/**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */&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;guitars&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;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Guitar&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run your tests, and if you followed along with everything, you should have two passing tests!&lt;/p&gt;

&lt;p&gt;These tests to me are worth their weight in gold. Your application is going to grow and get more complex. Stick with adding your factories and then testing the relationships. You have tests to make sure that things are never failing. Once you get into a good flow, it takes 10 minutes to set up a factory, then build your test, and the relationship. Once your tests pass, you are good to write any logic surrounding these relationships.&lt;/p&gt;

&lt;p&gt;You are going to thank yourself when you build a more complex relationship. Polymorphic many to many relationships are difficult to grasp but easy to test. The tests however, are the same as above. You now know that you set up your database schema right. You get the peace of mind knowing that the code you wrote, and are going to push to production on Friday, is at least tested. 😉&lt;/p&gt;

</description>
      <category>testing</category>
      <category>laravel</category>
      <category>lumen</category>
      <category>php</category>
    </item>
  </channel>
</rss>
