Model 使用laravel 5.3中的雄辩连接两个模型

Model 使用laravel 5.3中的雄辩连接两个模型,model,eloquent,relationship,laravel-5.3,Model,Eloquent,Relationship,Laravel 5.3,我有两张桌子 用户表格: id (integer) name (string) email (string) phone (string) codeMeli (string) userId (integer) adminId (integer) 和用户管理表格: id (integer) name (string) email (string) phone (string) codeMeli (string) userId (integer) adminId (inte

我有两张桌子

用户表格:

 id (integer)
 name (string)
 email (string)
 phone (string)
 codeMeli (string)
userId (integer)
adminId (integer)
用户管理表格:

 id (integer)
 name (string)
 email (string)
 phone (string)
 codeMeli (string)
userId (integer)
adminId (integer)
注意adminid字段保存另一用户的管理员用户id。一个用户可以是多个用户的管理员,或者一个用户可能有多个管理员。例如:

| 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 |
我使用OueryBuilder尝试了这个查询,效果很好:

Users::select('id','name','email','codeMeli','phone')
       ->join('user_admin','user_admin.userid','=','users.id')
       ->whereRaw("name LIKE '%".$name."%' and email LIKE '%".$email."%' and codeMeli LIKE '%".$codeMeli."%' and phone LIKE '%".$phone."%' and user_admin.adminid=".$CurrentUSerID)
       ->get();
但我想用雄辩的语言

这是我的用户型号:

<?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->belongsToMany('App\UserAdmin','id','userid');
    }
}
<?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->belongsToMany('App\Users','id','adminid');
    }
}

您可以将一个新的模型制作为与用户表相关的“管理”,并使用如下关系:

用户型号:

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


    public function userAdmin()
    {
        return $this->belongsToMany('App\Admin', 'user_admin', 'adminid', 'userid');
    }
}
class Admin extends Model
{
    protected $table = 'users';
    public $timestamps = false;
    protected $fillable = ['name','phone','email','codeMeli','admin'];
    public function userAdmin()
    {
        return $this->belongsToMany('App\Users', 'user_admin', 'userid', 'adminid');
    }
}
管理型号:

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


    public function userAdmin()
    {
        return $this->belongsToMany('App\Admin', 'user_admin', 'adminid', 'userid');
    }
}
class Admin extends Model
{
    protected $table = 'users';
    public $timestamps = false;
    protected $fillable = ['name','phone','email','codeMeli','admin'];
    public function userAdmin()
    {
        return $this->belongsToMany('App\Users', 'user_admin', 'userid', 'adminid');
    }
}
在您的控制器中

$user = Users::find($CurrentUSerID)->userAdmin()->get();
看看