title: Laravel 5 add results from query in a foreach to array
body: <p>I have a problem in my Laravel 5 application. 
  My controller-method contains a foreach, which should iterate over a previous Eloquent-result-array (it is named $zipcodes). So each zipcode is used to fill a query, which result returns an Article-object (named $article). 
  Every end of iteration it should add the Article-result to an array ($articles), which is used at the end of my method to display the Articles on my page.
  Maybe there is a better option for the performing of the Article-query, not every iteration, but I don't know how.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This is working for my problem, without perform the query in a foreach: <a href="https://stackoverflow.com/questions/33719951/laravel-multiple-where-clauses-in-query-from-given-array">Laravel multiple where clauses in query from given array</a></p>
  
  <p>My code now is</p>
  
  <pre><code>// grabs all zipcodes matching the distance
  $zipcodes = $this-&gt;getZipcodes($zipCoordinateId, $distance);
  
  foreach ($zipcodes AS $key=&gt;$val)
  {
      $zipcodes[$key] = (array) $val;
  }
  
  $codes = array_column($zipcodes, 'zc_zip');
  
  $articles = Article::whereIn('zipcode', $codes)-&gt;get();
  
  return view('pages.intern.articles.index', compact('articles'));
  </code></pre>
  
  <p><strong>UPDATE END</strong></p>
  
  <p>My SearchController:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Article;
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\DB;
  use Illuminate\Support\Facades\Session;
  
  class SearchController extends Controller
  {
  public function getSearch(Request $request)
  {
      if(($request-&gt;has('zipcode'))) {
          $zipcode = $request-&gt;input('zipcode');
          $distance = $request-&gt;input('distance');
  
          $zipCoordinateId = $this-&gt;getZipCoordindateId($zipcode);
          $zipcodes = $this-&gt;getZipcodes($zipCoordinateId, $distance);
  
          $articles = array();
  
          foreach($zipcodes as $value) {
              //$zipcode = $zipcodes[0]-&gt;zc_zip; // this is working
              $zipcode = $value-&gt;zc_zip;
  
              $article = Article::where('zipcode', $zipcode)-&gt;get();
  
  
             // add the $article each iteration to $articles
          }
  
          return view('pages.articles', compact('articles'));
      } else {
          return redirect('/articles');
      }
  }
  
  public function getZipCoordindateId($value) {
      $result = DB::table('zip_coordinates')
              -&gt;where('zc_zip', '=' , $value)
              -&gt;orWhere('zc_location_name', 'LIKE', $value)
              -&gt;pluck('zc_id');
      return $result;
  }
  
  public function getZipcodes($id, $distance) {
  
      $result = DB::select(
          DB::raw("
              SELECT dest.zc_zip,
                  ACOS(
                      SIN(RADIANS(src.zc_lat)) * SIN(RADIANS(dest.zc_lat))
                      + COS(RADIANS(src.zc_lat)) * COS(RADIANS(dest.zc_lat))
                      * COS(RADIANS(src.zc_lon) - RADIANS(dest.zc_lon))
                  ) * 6380 AS distance
              FROM zip_coordinates AS dest
              CROSS JOIN zip_coordinates AS src
              WHERE src.zc_id = :id
              HAVING distance &lt; :distance
              ORDER BY distance"), array('id' =&gt; $id, 'distance' =&gt; $distance));
  
      return $result;
  }
  }
  </code></pre>
  
  <p>my view:</p>
  
  <pre><code>@extends('layouts.default')
  
  @section('content')
      &lt;h1&gt;All Articles&lt;/h1&gt;
  &lt;hr/&gt;
  
  &lt;search&gt;
      {!! Form::open(['url' =&gt; 'intern/search']) !!}
          {!! Form::text('zipcode', null, ['class' =&gt; 'form-control']) !!}
          {!! Form::select('distance', [
            '5' =&gt; '5 km',
            '10' =&gt; '10 km',
            '25' =&gt; '25 km',
            '50' =&gt; '50 km',
            ], null, ['class' =&gt; 'form-control']) !!}
          {!! Form::submit('Suchen', ['class' =&gt; 'btn btn-success']) !!}
      {!! Form::close() !!}
  &lt;/search&gt;
  
  
  @foreach($articles as $article)
      &lt;article&gt;
          &lt;h2&gt;
              &lt;a href="{{ url('/articles', $article-&gt;id) }}"&gt;{{ $article-&gt;name }}&lt;/a&gt;
          &lt;/h2&gt;
  
          &lt;div&gt;
              &lt;label&gt;Description:&lt;/label&gt;
              &lt;p&gt;{{ $article-&gt;description }}&lt;/p&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;label&gt;ZIPCODE:&lt;/label&gt;
              {{ $article-&gt;zipcode }}
          &lt;/div&gt;
          &lt;div&gt;
              &lt;label&gt;Published:&lt;/label&gt;
              {{ $article-&gt;published }}
          &lt;/div&gt;
          &lt;div&gt;
              &lt;label&gt;Rank:&lt;/label&gt;
              {{ $article-&gt;rank }}
          &lt;/div&gt;
      &lt;/article&gt;
      &lt;hr/&gt;
  @endforeach
  @stop
  </code></pre>
  
  <p>Hope you can help me, thank you in advance.
  quantatheist</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>When I use</p>
  
  <pre><code>foreach($zipcodes as $key =&gt; $value)
          {
              $articles[] = Article::where('zipcode', $value-&gt;zc_zip)-&gt;get();
          }
          print_r($articles);
  </code></pre>
  
  <p>With another zipcode in the search and updated zipcodes of my articles, it prints (at the moment, there are three articles in my database, the both first have zipcodes, which are in the range of my new zipcode-search):</p>
  
  <pre><code>Array
  (
  [0] =&gt; Illuminate\Database\Eloquent\Collection Object
      (
          [items:protected] =&gt; Array
              (
                  [0] =&gt; App\Article Object
                      (
                          [fillable:protected] =&gt; Array
                              (
                                  [0] =&gt; name
                                  [1] =&gt; description
                                  [2] =&gt; profile
                                  [3] =&gt; driverlicense
                                  [4] =&gt; zipcode
                                  [5] =&gt; published
                                  [6] =&gt; rank
                                  [7] =&gt; contact
                                  [8] =&gt; note
                                  [9] =&gt; pictures
                                  [10] =&gt; publisher
                              )
  
                          [connection:protected] =&gt; 
                          [table:protected] =&gt; 
                          [primaryKey:protected] =&gt; id
                          [perPage:protected] =&gt; 15
                          [incrementing] =&gt; 1
                          [timestamps] =&gt; 1
                          [attributes:protected] =&gt; Array
                              (
                                  [id] =&gt; 5
                                  [name] =&gt; Test1
                                  [description] =&gt; Lorem ipsum dolor sit amet, consetetur 
                                  [profile] =&gt; Lorem ipsum dolor sit amet, consetetur
                                  [driverlicense] =&gt; yes
                                  [published] =&gt; no
                                  [rank] =&gt; 10
                                  [contact] =&gt; 1
                                  [pictures] =&gt; 
                                  [zipcode] =&gt; 89429
                                  [note] =&gt; 
                                  [publisher] =&gt; 1
                                  [created_at] =&gt; 2015-11-04 01:45:18
                                  [updated_at] =&gt; 2015-11-04 10:07:24
                              )
  
                          [original:protected] =&gt; Array
                              (
                                  [id] =&gt; 5
                                  [name] =&gt; Test1
                                  [description] =&gt; Lorem ipsum dolor sit amet, consetetur
                                  [profile] =&gt; Lorem ipsum dolor sit amet, consetetur
                                  [driverlicense] =&gt; yes
                                  [published] =&gt; no
                                  [rank] =&gt; 10
                                  [contact] =&gt; 1
                                  [pictures] =&gt; 
                                  [zipcode] =&gt; 89429
                                  [note] =&gt; 
                                  [publisher] =&gt; 1
                                  [created_at] =&gt; 2015-11-04 01:45:18
                                  [updated_at] =&gt; 2015-11-04 10:07:24
                              )
  
                          [relations:protected] =&gt; Array
                              (
                              )
  
                          [hidden:protected] =&gt; Array
                              (
                              )
  
                          [visible:protected] =&gt; Array
                              (
                              )
  
                          [appends:protected] =&gt; Array
                              (
                              )
  
                          [guarded:protected] =&gt; Array
                              (
                                  [0] =&gt; *
                              )
  
                          [dates:protected] =&gt; Array
                              (
                              )
  
                          [dateFormat:protected] =&gt; 
                          [casts:protected] =&gt; Array
                              (
                              )
  
                          [touches:protected] =&gt; Array
                              (
                              )
  
                          [observables:protected] =&gt; Array
                              (
                              )
  
                          [with:protected] =&gt; Array
                              (
                              )
  
                          [morphClass:protected] =&gt; 
                          [exists] =&gt; 1
                          [wasRecentlyCreated] =&gt; 
                      )
  
              )
  
      )
  
  [1] =&gt; Illuminate\Database\Eloquent\Collection Object
      (
          [items:protected] =&gt; Array
              (
                  [0] =&gt; App\Article Object
                      (
                          [fillable:protected] =&gt; Array
                              (
                                  [0] =&gt; name
                                  [1] =&gt; description
                                  [2] =&gt; profile
                                  [3] =&gt; driverlicense
                                  [4] =&gt; zipcode
                                  [5] =&gt; published
                                  [6] =&gt; rank
                                  [7] =&gt; contact
                                  [8] =&gt; note
                                  [9] =&gt; pictures
                                  [10] =&gt; publisher
                              )
  
                          [connection:protected] =&gt; 
                          [table:protected] =&gt; 
                          [primaryKey:protected] =&gt; id
                          [perPage:protected] =&gt; 15
                          [incrementing] =&gt; 1
                          [timestamps] =&gt; 1
                          [attributes:protected] =&gt; Array
                              (
                                  [id] =&gt; 6
                                  [name] =&gt; Test2
                                  [description] =&gt; Lorem ipsum dolor sit amet, consetetur 
                                  [profile] =&gt; Lorem ipsum dolor sit amet
                                  [driverlicense] =&gt; yes
                                  [published] =&gt; no
                                  [rank] =&gt; 3
                                  [contact] =&gt; 1
                                  [pictures] =&gt; 
                                  [zipcode] =&gt; 89428
                                  [note] =&gt; 
                                  [publisher] =&gt; 1
                                  [created_at] =&gt; 2015-11-04 01:45:38
                                  [updated_at] =&gt; 2015-11-04 09:58:01
                              )
  
                          [original:protected] =&gt; Array
                              (
                                  [id] =&gt; 6
                                  [name] =&gt; Test2
                                  [description] =&gt; Lorem ipsum dolor sit amet, consetetur 
                                  [profile] =&gt; Lorem ipsum dolor sit amet
                                  [driverlicense] =&gt; yes
                                  [published] =&gt; no
                                  [rank] =&gt; 3
                                  [contact] =&gt; 1
                                  [pictures] =&gt; 
                                  [zipcode] =&gt; 89428
                                  [note] =&gt; 
                                  [publisher] =&gt; 1
                                  [created_at] =&gt; 2015-11-04 01:45:38
                                  [updated_at] =&gt; 2015-11-04 09:58:01
                              )
  
                          [relations:protected] =&gt; Array
                              (
                              )
  
                          [hidden:protected] =&gt; Array
                              (
                              )
  
                          [visible:protected] =&gt; Array
                              (
                              )
  
                          [appends:protected] =&gt; Array
                              (
                              )
  
                          [guarded:protected] =&gt; Array
                              (
                                  [0] =&gt; *
                              )
  
                          [dates:protected] =&gt; Array
                              (
                              )
  
                          [dateFormat:protected] =&gt; 
                          [casts:protected] =&gt; Array
                              (
                              )
  
                          [touches:protected] =&gt; Array
                              (
                              )
  
                          [observables:protected] =&gt; Array
                              (
                              )
  
                          [with:protected] =&gt; Array
                              (
                              )
  
                          [morphClass:protected] =&gt; 
                          [exists] =&gt; 1
                          [wasRecentlyCreated] =&gt; 
                      )
  
              )
  
      )
  
  [2] =&gt; Illuminate\Database\Eloquent\Collection Object
      (
          [items:protected] =&gt; Array
              (
              )
  
      )
  
  )
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I print_r the articles from $articles = Article::all();, which uses the same return view('pages.articles', compact('articles')); and displays on the same site:</p>
  
  <pre><code>Illuminate\Database\Eloquent\Collection Object
  (
  [items:protected] =&gt; Array
      (
          [0] =&gt; App\Article Object
              (
                  [fillable:protected] =&gt; Array
                      (
                          ...
                      )
  
                  [connection:protected] =&gt; 
                  [table:protected] =&gt; 
                  [primaryKey:protected] =&gt; id
                  [perPage:protected] =&gt; 15
                  [incrementing] =&gt; 1
                  [timestamps] =&gt; 1
                  [attributes:protected] =&gt; Array
                      (
                          ...
                      )
  
                  [original:protected] =&gt; Array
                      (
                          ...
                      )
  
                  [relations:protected] =&gt; Array
                      (
                      )
  
                  [hidden:protected] =&gt; Array
                      (
                      )
  
                  [visible:protected] =&gt; Array
                      (
                      )
  
                  [appends:protected] =&gt; Array
                      (
                      )
  
                  [guarded:protected] =&gt; Array
                      (
                          [0] =&gt; *
                      )
  
                  [dates:protected] =&gt; Array
                      (
                      )
  
                  [dateFormat:protected] =&gt; 
                  [casts:protected] =&gt; Array
                      (
                      )
  
                  [touches:protected] =&gt; Array
                      (
                      )
  
                  [observables:protected] =&gt; Array
                      (
                      )
  
                  [with:protected] =&gt; Array
                      (
                      )
  
                  [morphClass:protected] =&gt; 
                  [exists] =&gt; 1
                  [wasRecentlyCreated] =&gt; 
              )
  
          [1] =&gt; App\Article Object
              (
                  [fillable:protected] =&gt; Array
                      (
                          ..
                      )
  
                  [connection:protected] =&gt; 
                  [table:protected] =&gt; 
                  [primaryKey:protected] =&gt; id
                  [perPage:protected] =&gt; 15
                  [incrementing] =&gt; 1
                  [timestamps] =&gt; 1
                  [attributes:protected] =&gt; Array
                      (
                          ...
                      )
  
                  [original:protected] =&gt; Array
                      (
                          ..
                      )
  
                  [relations:protected] =&gt; Array
                      (
                      )
  
                  [hidden:protected] =&gt; Array
                      (
                      )
  
                  [visible:protected] =&gt; Array
                      (
                      )
  
                  [appends:protected] =&gt; Array
                      (
                      )
  
                  [guarded:protected] =&gt; Array
                      (
                          [0] =&gt; *
                      )
  
                  [dates:protected] =&gt; Array
                      (
                      )
  
                  [dateFormat:protected] =&gt; 
                  [casts:protected] =&gt; Array
                      (
                      )
  
                  [touches:protected] =&gt; Array
                      (
                      )
  
                  [observables:protected] =&gt; Array
                      (
                      )
  
                  [with:protected] =&gt; Array
                      (
                      )
  
                  [morphClass:protected] =&gt; 
                  [exists] =&gt; 1
                  [wasRecentlyCreated] =&gt; 
              )
  
          [2] =&gt; App\Article Object
              (
                  [fillable:protected] =&gt; Array
                      (
                          ..
                      )
  
                  [connection:protected] =&gt; 
                  [table:protected] =&gt; 
                  [primaryKey:protected] =&gt; id
                  [perPage:protected] =&gt; 15
                  [incrementing] =&gt; 1
                  [timestamps] =&gt; 1
                  [attributes:protected] =&gt; Array
                      (..
                      )
  
                  [original:protected] =&gt; Array
                      (
                          ..
                      )
  
                  [relations:protected] =&gt; Array
                      (
                      )
  
                  [hidden:protected] =&gt; Array
                      (
                      )
  
                  [visible:protected] =&gt; Array
                      (
                      )
  
                  [appends:protected] =&gt; Array
                      (
                      )
  
                  [guarded:protected] =&gt; Array
                      (
                          [0] =&gt; *
                      )
  
                  [dates:protected] =&gt; Array
                      (
                      )
  
                  [dateFormat:protected] =&gt; 
                  [casts:protected] =&gt; Array
                      (
                      )
  
                  [touches:protected] =&gt; Array
                      (
                      )
  
                  [observables:protected] =&gt; Array
                      (
                      )
  
                  [with:protected] =&gt; Array
                      (
                      )
  
                  [morphClass:protected] =&gt; 
                  [exists] =&gt; 1
                  [wasRecentlyCreated] =&gt; 
              )
  
      )
  
  )
  </code></pre>
  
  <p>So the output from my print_r($articles) should be something like this.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>My current foreach is:</p>
  
  <pre><code>foreach($zipcodes as $key =&gt; $value)
          {
              //$code = $zipcodes[0]-&gt;zc_zip; // working, prints out one article
              //$article = Article::where('zipcode', $code)-&gt;get();
              $article = Article::where('zipcode', $value-&gt;zc_zip)-&gt;get();
              //print_r($article);
          }
          print_r($article);
  </code></pre>