title: should I inject repository class or model class in controller's constructor?
body: <p>I hope you will read this and explain it to me in a very sensible way. I'd really much appreciate it. I know it's a lot ,but still thank you.</p>
  
  <p>Let's say I have a PostController controller and I have A post model. Here are following scenarios how I can write my code.</p>
  
  <p>1) I can have a constructor in a controller and inject Post model there. like this:</p>
  
  <pre><code>class PostController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      $post;
      public function __construct(Post $post){
          $this-&gt;post = $post;
      }
  
      public function show($id){
          return $this-&gt;post-&gt;find($id);
      }
  </code></pre>
  
  <p>2) I can directly write Post model in a function (show function).</p>
  
  <pre><code>class PostController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
  
  
      public function show($id){
          return Post::find($id);
      }
  </code></pre>
  
  <p>3) I can have a repository class which extends my eloquent Post Model and inject it into constructor.</p>
  
  <pre><code>class PostRepository extends Post{
  
  
  }
  
  class PostController extends Controller{
  
      protected $post;
      public function __construct(PostRepository $post){
          $this-&gt;postRepo = $post;
      }
  
      public function show($id){
          return $this-&gt;postRepo-&gt;find($id);
      }
  }
  </code></pre>
  
  <p>4) I can have postRepository without injecting and directly using it.</p>
  
  <pre><code>class PostController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
  
  
      public function show($id){
          return PostRepository::find($id);
      }
  </code></pre>
  
  <p>I hope you understand all of these examples. Let's talk about my question and how I look at it. Before I start talking, I want you to know that I want my code to be testable and well-written. </p>
  
  <p>Question 1) let's say I use the second example. I'm directly accessing Post model there. It's testable because laravel provides a way to mock eloquent models. Why is this bad approach? I know it's bad, i just don't know why, because I can still mock eloquent and test it.</p>
  
  <p>Question 2) What's the difference between the second and the first example? If I can test it and mock an eloquent model if it's directly accessed in a function, why to inject it in a constructor at all?</p>
  
  <p>Question 3) Let's say I don't use repository patterns. Creating repository classes doesn't mean using repository pattern. Repository pattern is when used interfaces and you can swap (for example from eloquent to other ORM). Let's say I always know that I will use only eloquent and I don't want to decouple my code from framework itself. Then the question is why to use Repository Classes at all as shown in the third and fourth example? I'm asking this because people say that it's better to put complex logic in repositories and not in a model.</p>
  
  <p>Question 4) What's the difference between the third and fourth example? I can still test the fourth example. why to inject PostRepository in a constructor at all?</p>