在 laravel 5.3 中使用 Eloquent 加入两个模型
Join two Models using Eloquent in laravel 5.3
我有两个table。
用户 table:
id (integer)
name (string)
email (string)
phone (string)
codeMeli (string)
和user_admin table:
userId (integer)
adminId (integer)
注意:adminid字段保留了另一个用户的admin用户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 作为与 users 相关的“Admin” table 并使用这样的关系:
用户 型号:
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
我有两个table。
用户 table:
id (integer)
name (string)
email (string)
phone (string)
codeMeli (string)
和user_admin table:
userId (integer)
adminId (integer)
注意:adminid字段保留了另一个用户的admin用户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 作为与 users 相关的“Admin” table 并使用这样的关系:
用户 型号:
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