title: Laravel Custom Model Methods
body: <p>Whenever I add additional logic to Eloquent models, I end up having to make it a <code>static</code> method (i.e. less than ideal) in order to call it from the model's facade. I've tried searching a lot on how to do this the proper way and pretty much all results talk about creating methods that return portions of a Query Builder interface. I'm trying to figure out how to add methods that can return anything and be called using the model's facade.</p>
  
  <p>For example, lets say I have a model called <code>Car</code> and want to get them all:</p>
  
  <pre><code>$cars = Car::all();
  </code></pre>
  
  <p>Great, except for now, let's say I want to sort the result into a multidimensional array by make so my result may look like this:</p>
  
  <pre><code>$cars = array(
    'Ford' =&gt; array(
       'F-150' =&gt; '...',
       'Escape' =&gt; '...',
    ),
    'Honda' =&gt; array(
       'Accord' =&gt; '...',
       'Civic' =&gt; '...',
    ),
  );
  </code></pre>
  
  <p>Taking that theoretical example, I am tempted to create a method that can be called like:</p>
  
  <pre><code>$cars = Car::getAllSortedByMake();
  </code></pre>
  
  <p>For a moment, lets forget the terrible method name and the fact that it is tightly coupled to the data structure. If I make a method like this in the model:</p>
  
  <pre><code>public function getAllSortedByMake()
  {
     // Process and return resulting array
     return array('...');
  }
  </code></pre>
  
  <p>And finally call it in my controller, I will get this Exception thrown:</p>
  
  <blockquote>
    <p>Non-static method Car::getAllSortedByMake() should not be called statically, assuming $this from incompatible context</p>
  </blockquote>
  
  <p><strong>TL;DR</strong>: How can I add custom functionality that makes sense to be in the model without making it a static method and call it using the model's facade?</p>
  
  <hr>
  
  <h2>Edit:</h2>
  
  <p>This is a theoretical example. Perhaps a rephrase of the question would make more sense. Why are certain non-static methods such as <code>all()</code> or <code>which()</code> available on the facade of an Eloquent model, but not additional methods added into the model? This means that the <code>__call</code> magic method is being used, but how can I make it recognize my own functions in the model?</p>
  
  <p>Probably a better example over the "sorting" is if I needed to run an calculation or algorithm on a piece of data:</p>
  
  <pre><code>$validSPG = Chemical::isValidSpecificGravity(-1.43);
  </code></pre>
  
  <p>To me, it makes sense for something like that to be in the model as it is domain specific.</p>