title: How to set up eloquent relationships to query table where relationship between two tables exists in a third table
body: <p>I'm using Laravel 5.7 and mysql. I need help setting up my eloquent relationships between 3 tables.</p>
  
  <p>Locations</p>
  
  <pre><code>id
  city
  state
  </code></pre>
  
  <p>Services </p>
  
  <pre><code>id
  name
  </code></pre>
  
  <p>Allowed_Locations</p>
  
  <pre><code>location_id (id from Locations table)
  service_id (id from Services table)
  </code></pre>
  
  <p>Locations </p>
  
  <pre><code>id, city, state
  1, San Diego, California
  2, Seattle, Washington 
  3, Portland, Oregon 
  </code></pre>
  
  <p>Services </p>
  
  <pre><code>id, name 
  1, Tax Services
  2, Legal Services
  </code></pre>
  
  <p>Allowed Locations </p>
  
  <pre><code>location_id, service_id
  1, 1
  1, 2
  3, 2
  </code></pre>
  
  <p>Basically one service can be available in many locations. I want to be able to query services, along with the locations the service is available in</p>
  
  <p>Heres an example of what I am looking to return. I know how to do this using MySQL but I'm not sure how to set up eloquent relationships </p>
  
  <pre><code>{
      {
          id: 1,
          name: Tax Services
          locations: 
          {
              {
                  id: 1,
                  city: San Diego,
                  state: California,
              },
              {
                  id: 2,
                  city: Seattle,
                  state: Washington,
              },
              {
                  id: 3,
                  city: Seattle,
                  state: Washington,
              },
          }
      },
      {
          id: 2,
          name: Legal Services
          locations: 
          {
              {
                  id: 3,
                  city: Portland,
                  state: Oregon,
              },
          }   
      }
  }
  </code></pre>