title: Laravel Model - Calculate avg ratings
body: <p>I have table "Orders" where rating score is stored, and inside that table i have relation with places.</p>
  
  <p>I need to grab order rating from each place and calculate the average rating score. I need it on model so I can grab it for later use.</p>
  
  <p>This is the scheme:</p>
  
  <p><strong>[ORDERS]</strong></p>
  
  <p>--order_id = 10</p>
  
  <p>--place_id = 2</p>
  
  <p>--rating = 3</p>
  
  <p><strong>[PLACES]</strong></p>
  
  <p>--place_id = 2</p>
  
  <p>So, when i call: $place->avgRating(),it needs to return 4.3 for example. Pulling the order information is working. Here is the code:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Place extends Model
  {
      public function user(){
        return $this-&gt;belongsTo('App\User');
      }
  
      public function orders(){
        return $this-&gt;hasMany('App\Order');
      }
  
  
  
  }
  </code></pre>