【问题标题】:Join two Models using Eloquent in laravel 5.3在 laravel 5.3 中使用 Eloquent 连接两个模型
【发布时间】:2017-03-11 08:04:09
【问题描述】:

我有两张桌子。

用户表:

 id (integer)
 name (string)
 email (string)
 phone (string)
 codeMeli (string)

user_admin表:

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();

但我想使用 Eloquent。

这是我的用户模型:

<?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');
    }
}

user_admin型号:

<?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');
    }
}

我应该如何使用 Eloquent 编写这个查询?我是否在模型中以正确的方法使用关系?我认为我的模型应该被编辑,但我不知道如何。 感谢您的帮助。

【问题讨论】:

    标签: model eloquent relationship laravel-5.3


    【解决方案1】:

    您可以创建一个与 users 表相关的新 Model 作为“Admin”,并使用如下关系:

    用户模型:

    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();
    

    看看https://laravel.com/docs/5.3/eloquent-relationships#many-to-many

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 2019-10-04
      • 2019-10-24
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多