title: Get object value on an array iteration with foreach
body: <p>In the following very simplified example and study case, we get the list of posts and the count of comments for each post.</p>
  
  <p>However, I intended to send to client side only the value, i.e. <strong>2</strong>, and not the array object <strong>[{total: 2}]</strong></p>
  
  <p>Is there a simplified way to do this with the laravel?</p>
  
  <p><em>Note: DB::select must be used</em></p>
  
  <pre><code>public function readPosts(Request $request) {     
      $posts = DB::select("SELECT * FROM posts");   
  
       foreach ($posts as $key =&gt; $post) {         
           $idComment = $post-&gt;id_comment;             
           $post-&gt;nComments = DB::select('SELECT COUNT(id_comment) As total FROM post_comments WHERE id_comment = ? ', [$idComment]); 
       }    
  
      if($posts){
        return response()-&gt;json(['success'=&gt;true, "output"=&gt;$posts);
      }else{
        return response()-&gt;json(['success'=&gt;false]);  
      }
  }
  </code></pre>
  
  <p><strong>response</strong></p>
  
  <pre><code>data:
      0:
        id_post: 1
        post: "post text"
        nComments: [{total:2}]
     1:
        id_post: 2
        post: "post text 2"
        nComments: [{total:1}]
  </code></pre>
  
  <p><strong>Expected</strong></p>
  
  <pre><code>data:
      0:
        id_post: 1
        post: "post text"
        nComments: 2
      1:
        id_post: 2
        post: "post text 2"
        nComments: 1
  </code></pre>