title: how to sort results based on pivot table in laravel
body: <p>i have three table like this :</p>
  
  <ul>
  <li>tour (id name) -> assume (id:1 , name : something)</li>
  <li>meta (id key) ->assume (id:1 , key: tour_duration)</li>
  <li>meta_tour (tour_id meta_id value) ->assume (tour_id:1 , meta_id:1 , value: 15)</li>
  </ul>
  
  <p>it's ManyToMany relationship.</p>
  
  <p>i want to sort tours based on meta value in meta_tour table like this :</p>
  
  <pre><code>tours orderBy tour_duration.value
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>i tried this :</p>
  
  <pre><code>        Tour::with(['metas' =&gt; function ($query) {
          $query-&gt;where('key', 'tour_duration')
          -&gt;orderByDesc('value');
          }])-&gt;paginate(15);
  </code></pre>
  
  <p>and this :</p>
  
  <pre><code>        Tour::with(['metas' =&gt; function ($query) {
          $query-&gt;where('key', 'tour_duration');
          }])-&gt;orderByDesc('value')-&gt;paginate(15);
  </code></pre>
  
  <p>and this : </p>
  
  <pre><code>        Tour::whereHas('metas' , function ($query) {
          $query-&gt;where('key', 'tour_duration')
                -&gt;orderBy('value', 'desc')
          })-&gt;paginate(15);
  </code></pre>
  
  <p>but not working.</p>