title: How to match data from two different database tables and display it
body: <p>I am looking for a solution which would withdraw essential data used to match other data from a different table. Then I would like to display this data inside a blade in a form of a table.</p>
  
  <p>Inside the database, I have a "matching" entity which stores user's credentials which I would like to use for matching (for example desired price of the product). Entity contains "peopleID" as "matching" belongs to website users. When user is created, we assign matching options which are getting stored inside that "matching" entity. The number of rows inside Matching entity depends on the number of counties chosen during user creation stage.</p>
  
  <p>I know that to withdraw matching data from the database I need to use a foreach loop.</p>
  
  <p>The problem I have is when I output data inside the blade. For some reason it matches products only with the last item from an array. It should match prodtucts with all matching credentials.</p>
  
  <p>Code:</p>
  
  <pre><code>        $matchings = Match::where('PeopleID', '=', $id)-&gt;get();
          $sales = DB::table('sales');
  
          foreach ($matchings as $matching)
          {
              $county = $matching-&gt;county;
              $sales-&gt;where('county', '=', $county);
  
          }
  
          $results = $sales-&gt;get();
  </code></pre>
  
  <p>So for one of the customers I have two matchings with different "counties". It only displays data for the last one added. How could I make it display data for other matching which contains a different county. I hope you know what I mean.</p>
  
  <p>Thanks for any help.</p>
  
  <p>Update - Major part of the code is done. Thank you for your help.</p>
  
  <p>The second question is about adding the rest of matching options. As stated before the number of matches depends on the number of counties added. Each match has its own attributes. The idea is to show matched results for each county.</p>
  
  <p>I know I will need some if statements to do this. </p>
  
  <p>Here is an example which I would like to implement:</p>
  
  <pre><code>            $maxprice = $match-&gt;maxprice;
              $minprice = $match-&gt;minprice;
              $brand_a = $match-&gt;brand_a;
              $brand_b = $match-&gt;brand_b;
  
              if($maxprice != '0')
              {
                  $sales = DB::table('sales')-&gt;where('maxprice', '&lt;=', $maxprice);
              }
  
              if($minprice != '0')
              {
                  $sales = DB::table('sales')-&gt;where('minprice', '&gt;=', $minprice);
              }
  
              if($brand_a == '1')
              {
                  $sales = DB::table('sales')-&gt;where('brand_a', '1');
              }
  
              if($brand_b == '1')
              {
                  $sales = DB::table('sales')-&gt;where('brand_b', '1');
              }
  </code></pre>
  
  <p>To this code:</p>
  
  <pre><code>$user = User::find($id); // get our User with the Id (person id?)
  
  $matches = $user-&gt;matches; // get all the matches
  
  // or you could one line the above: $matches = User::find($id)-&gt;matches;
  
  // get all the counties in the returned matches, could use pluck() method in    higher version of laravel
  $counties = [];
  
  foreach($matches as $match) {
      $counties[] = $match-&gt;county;
  }
  
  $results = DB::table('sales')-&gt;whereIn('county', $counties)-&gt;get();
  </code></pre>
  
  <p>Many Thanks for your help!</p>
  
  <p>@update</p>
  
  <p>Relationships:</p>
  
  <p>Match:</p>
  
  <pre><code>public function people()
  {
      return $this-&gt;belongsTo('People', 'PeopleID', 'PeopleID');
  }
  </code></pre>
  
  <p>People:</p>
  
  <pre><code>public function matches()
  {
      return $this-&gt;hasMany('Match', 'PeopleID', 'PeopleID');
  }
  </code></pre>
  
  <p>I have a connection between those as Match holds people's "search" credentials. The first solution which you have provided works perfectly. Now, this solution filtered out sales by county which is a good move as now they need to be filtered by minimum and maximum price (minprice, maxprice) and other credentials such as brand_a and brand_b.</p>
  
  <p>The idea of brand_a and brand_b:
  Checkboxes are responsible for changing brand_a and brand_b value inside Matching. If these are checked the values inside Matching entity become '1'. If these are not checked they become '0' which means that sales don't have to be filtered out by those values. </p>
  
  <p>Sales entity contains "Holdings" attribute. The value of "Holdings" can be brand_a or brand_b. Sales also contains "Price".</p>
  
  <p>So, to make this clear:</p>
  
  <p>Sale Entity contains: SaleID, Price, Holdings, County.</p>
  
  <p>Holdings are values: brand_a or brand_b.
  Price is just a number.
  County is plain text.</p>
  
  <p>Matching Entity contains: PeopleID, MinimumPrice, MaximumPrice, brand_a, brand_b, county.</p>
  
  <p>PeopleID is a foreign key. We need to know which matching belongs to what user.
  (there can be multiple matchings for one user depending on the number of counties chosen).
  MinimumPrice and MaximumPrice are numbers.
  brand_a and brand_b are the values (1 or 0) depending if the checkboxes were checked.
  County is the name of a county.</p>
  
  <p>Now, if person 1543 (peopleID = 1543) contains 3 matchings, each containing different search credentials.</p>
  
  <p>1st: 
  PeopleID: 1543
  MinimumPrice: 1000
  MaximumPrice: 7000
  brand_a: 0
  brand_b: 1
  county: county_a</p>
  
  <p>2nd:
  PeopleID: 1543
  MinimumPrice: 2500
  MaximumPrice: 10000
  brand_a: 1
  brand_b: 1
  county: county_f</p>
  
  <p>3rd:
  PeopleID: 1543
  MiniumPrice: 2000
  MaximumPrice:9500
  brand_a: 0
  brand_b: 0
  county: county_d</p>
  
  <p>I need to match this data against the data that is inside the Sales. There can be over a 1,000 different sales with different prices etc. I just need to filter them and display Sales that are desired by the person based on person's matching.</p>
  
  <p>I hope this better presents you the situation. Thanks.</p>