title: Fetching has many relationship data Laravel and using avg function
body: <p>I am using Laravel 5 with vue js. Basically i am fetching data using axios and trying to display on the webpage using vue js v-for directive.
  i have tables in database like this:</p>
  
  <p><code>ratings Table</code></p>
  
  <p><code>id</code> <code>review_id</code> <code>rating</code></p>
  
  <p>Then i have a </p>
  
  <p><code>reviews table</code></p>
  
  <p><code>id</code> <code>review</code></p>
  
  <p>They have one to many relationship between. so here in my <code>Review</code> Model i have method </p>
  
  <pre><code> public function ratings()
      {
          return $this-&gt;hasMany('App\Rating')
              -&gt;selectRaw('review_id,AVG(rating) AS average_rating')
              -&gt;groupBy('review_id');
      }
  </code></pre>
  
  <p>so here i want to fetch list of reviews with their average ratings. so in my controller i am doing this:</p>
  
  <pre><code>  public function getAllReviews(Request $request)
      {
          $reviews = Review::with('ratings')-&gt;get();
  
          return $reviews;
      }
  </code></pre>
  
  <p>So i am getting result but the problem is every review doesnt have ratings record so it is returning null? maybe... 
  when i try to render in vue template it throws an error <code>undefined</code> because in our collection some reviews do not have ratings.</p>
  
  <p>Now my question is: Can i do something like if there is no record in the ratings for a particular review is it possible to add an array with value 0?? so in my frontend it wont see as undefined.</p>
  
  <p>I hope i am successful to explain i am trying.</p>
  
  <p>Thank you.</p>