title: Laravel 5: DB::table() and relationship in Eloquent
body: <p>I have a hasMany relationship between Community and LangCommunity and I am trying to sort by name field without losing the relationship in the view.</p>
  
  <p><strong>Model Community</strong></p>
  
  <pre><code>protected $fillable = ['province_id', 'active', 'address', 'phone', 'google_map'];
  
  public function langs()
  {
      return $this-&gt;hasMany('App\Models\LangCommunity');
  }
  </code></pre>
  
  <p><strong>Model LangCommunity</strong></p>
  
  <pre><code>protected $fillable = ['community_id', 'lang_id', 'name', 'text'];
  
  public function community()
  {
      return $this-&gt;belongsTo('App\Models\Community');
  }
  </code></pre>
  
  <p>In <strong>controller</strong>, I have:</p>
  
  <pre><code>$data['communities'] = DB::table('communities')
          -&gt;join('lang_communities', 'communities.id', '=', 'lang_communities.community_id')
          -&gt;select('communities.*', 'lang_communities.name')
          -&gt;where('lang_communities.lang_id', '1')
          -&gt;orderBy('lang_communities.name', 'asc')
          -&gt;paginate($num);
  </code></pre>
  
  <p>This work, but I can't use the relationship in the view. I have tried to do it in the following way:</p>
  
  <pre><code>$data['communities'] = Community::with(['langs' =&gt; function($query)
      {
          $query
              -&gt;where('lang_id', '1')
              -&gt;join('communities', 'communities.id', '=', 'lang_communities.community_id')
              -&gt;orderBy('lang_communities.name', 'asc');
      }])-&gt;paginate($num);
  </code></pre>
  
  <p>But this does not sort by name. Any idea?</p>