title: How to test laravel controller method?
body: <p>My controller looks like this: </p>
  
  <pre><code>public function store(Request $request) {
  
          $validateData = $request-&gt;validate([
              'name' =&gt; 'required|unique:languages',
              'code' =&gt; 'required|size:3',
              'flag' =&gt; 'required|size:2'
          ]);
  
          $language = new Language();
          $language-&gt;name = $request-&gt;name;
          $language-&gt;code = $request-&gt;code;
          $language-&gt;flag = $request-&gt;flag;
          $saveLanguage = $language-&gt;save();
  
          if(!$saveLanguage){
              return response()-&gt;json(['error'=&gt;'Something went wrong, please try later.'],500);
          }
  
          return response()-&gt;json(['success'=&gt;'Language has been created successfully', 'data'=&gt;$language],200);
  </code></pre>
  
  <p>As you can see, I am instantiating a new Language object and everything works fine, but first problem is, imagine I change Language class in future (for example: you have to pass 2 parameters in constructor), I have to change this controller and every other controllers where I am instantiating <code>Language</code> object.</p>
  
  <p>The second problem is I can't or it's too hard to test this controller.</p>
  
  <p>I am curious what is the best solution to solve this problems in laravel?</p>
  
  <p>For example is it a good solution to use simple factory or factory method pattern for every model I am using in my controllers.</p>
  
  <p>I think when you write something like this <code>$var = new SomeClass()</code> in other class, this <code>otherClass</code> is depends on SomeClass and when you want to change <code>SomeClass</code> you have to update <code>otherClass</code> to. What do you think abaout this, how can I avoid this. </p>