Join two Models using Eloquent in laravel 5.3
<p>I have two tables.</p>

<p><strong>users</strong> table:</p>

<pre><code> id (integer)
 name (string)
 email (string)
 phone (string)
 codeMeli (string)
</code></pre>

<p>and <strong>user_admin</strong> table:</p>

<pre><code>userId (integer)
adminId (integer)
</code></pre>

<p><strong>Note</strong>: <strong><em>adminid</em></strong> field keeps the user id that is admin of another user. And a user can be admin of multiple users or a user might have more than one admin. For example:</p>

<pre><code>| id | name | email | phone | codeMeli |            | userid | adminid |
 ---- ------ ------- ------- ----------              -------- ---------
| 5  | mike | m@yaho| 12345 | 12345678 |            |   6    |    5    |
| 6  | sara | s@yaho| 54321 | 87654321 |            |   7    |    5    |
| 7  | joe  | j@yaho| 56421 | 65875234 |            |   7    |    8    |
| 8  | sag  | s@yaho| 57635 | 64616843 |
</code></pre>

<p>I tried this query using <strong>OueryBuilder</strong> and works well:</p>

<pre><code>Users::select('id','name','email','codeMeli','phone')
       -&gt;join('user_admin','user_admin.userid','=','users.id')
       -&gt;whereRaw("name LIKE '%".$name."%' and email LIKE '%".$email."%' and codeMeli LIKE '%".$codeMeli."%' and phone LIKE '%".$phone."%' and user_admin.adminid=".$CurrentUSerID)
       -&gt;get();
</code></pre>

<p>But I want to use Eloquent. </p>

<p>And this is My <strong>user</strong> Model:</p>

<pre><code>&lt;?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    protected $table = 'users';
    public $timestamps = false;
    protected $fillable = ['name','phone','email','codeMeli','admin'];

    public function userAdmin()
    {
        return $this-&gt;belongsToMany('App\UserAdmin','id','userid');
    }
}
</code></pre>

<p>and <strong>user_admin</strong> Model:</p>

<pre><code>&lt;?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class UserAdmin extends Model
{
    protected $table = 'user_admin';
    public $timestamps = false;
    protected $fillable = ['userid','adminid'];

    public function users()
    {
       return $this-&gt;belongsToMany('App\Users','id','adminid');
    }
}
</code></pre>

<p>How should I write this query using <strong>Eloquent</strong>? Am I using relationships in a correct method in Models? I think my Models should be edited but I don't know how.
Thanks for your help.</p>