Eloquent 模型:使用子项上的标志定义一对一关系 table
Eloquent Model: Define on-to-one relationship using flag on child table
我有 2 tables vendors
和 partners
vendors
table只存储某公司的名称,partners
table存储为某公司工作的用户。所以结构是这样的:
供应商
| id | name |
+------+---------------+
| 1 | Vendor-1 |
| 2 | Vendor-2 |
| 3 | Vendor-3 |
合作伙伴
| id | user_name | password |vendor_id | is_owner | is_admin | is_technitian |
+----+------------+-----------+----------+------------+------------+---------------+
| 1 | abc | ^&ASKJHA | 1 | 1 | 1 | 0 |
| 2 | def | ^&ASKJHA | 2 | 1 | 1 | 0 |
| 3 | ghi | ^&ASKJHA | 1 | 0 | 1 | 0 |
| 4 | jkl | ^&ASKJHA | 3 | 1 | 1 | 0 |
| 5 | mno | ^&ASKJHA | 1 | 0 | 0 | 1 |
| 6 | pqr | ^&ASKJHA | 2 | 0 | 1 | 0 |
| 7 | stu | ^&ASKJHA | 1 | 0 | 0 | 1 |
| 8 | vwx | ^&ASKJHA | 2 | 0 | 0 | 1 |
| 9 | yz | ^&ASKJHA | 3 | 0 | 0 | 1 |
因此,正如您在上面看到的那样,一个合作伙伴是任何供应商的所有者,其余合作伙伴是供应商的雇员。
我正在使用 Eloquent ORM,我已经为合作伙伴和供应商定义了模型。我想在供应商模型中添加一个 owner
方法,这样我就可以直接访问任何供应商对象的所有者。我想知道的是如何在我的模型定义中关联它。它是可行的还是我需要对我的数据库结构进行一些更改?
class Vendor extends Model{
/**
* Get all the users for this vendor
*/
public function users(){
$this->hasMany(Partner::class);
}
public function owner(){
// how do i relate one owner from partner model who has is_owner == 1
}
}
试试 where()
public function owner(){
$this->hasOne(Partner::class)->where('is_owner', true);
}
您可能必须在关系中指定外键。
与其定义新的关系,并导致重复代码和破坏 DRY 原则,不如利用 Eloquent 中开箱即用的 Local Scopes ORM.
Local scopes allow you to define common sets of constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all users that are considered "popular". To define a scope, simply prefix an Eloquent model method with scope.
Scopes should always return a query builder instance:
例如:- 在您的 Vendor 模型中定义一个函数 ScopeOwner(),例如:
public function scopeOwner($query)
{
return $query->where('is_owner', 1);
}
然后在您的控制器中使用它或按如下方式实现它:
$vendor->users()->owner()->get();
Once the scope has been defined, you may call the scope methods when querying the model. However, you do not need to include the scope prefix when calling the method. You can even chain calls to various scopes.
接下来,您甚至可以使用动态作用域来更好地重用代码。
Sometimes you may wish to define a scope that accepts parameters. To get started, just add your additional parameters to your scope. Scope parameters should be defined after the $query parameter:
public function scopeOfType($query, $type)
{
return $query->where('is_owner', $type);
}
并按如下方式消费:
$vendor->users()->ofType(0)->get();
更多信息,查看官方文档:Eloquent ORM Scopes
我有 2 tables vendors
和 partners
vendors
table只存储某公司的名称,partners
table存储为某公司工作的用户。所以结构是这样的:
供应商
| id | name |
+------+---------------+
| 1 | Vendor-1 |
| 2 | Vendor-2 |
| 3 | Vendor-3 |
合作伙伴
| id | user_name | password |vendor_id | is_owner | is_admin | is_technitian |
+----+------------+-----------+----------+------------+------------+---------------+
| 1 | abc | ^&ASKJHA | 1 | 1 | 1 | 0 |
| 2 | def | ^&ASKJHA | 2 | 1 | 1 | 0 |
| 3 | ghi | ^&ASKJHA | 1 | 0 | 1 | 0 |
| 4 | jkl | ^&ASKJHA | 3 | 1 | 1 | 0 |
| 5 | mno | ^&ASKJHA | 1 | 0 | 0 | 1 |
| 6 | pqr | ^&ASKJHA | 2 | 0 | 1 | 0 |
| 7 | stu | ^&ASKJHA | 1 | 0 | 0 | 1 |
| 8 | vwx | ^&ASKJHA | 2 | 0 | 0 | 1 |
| 9 | yz | ^&ASKJHA | 3 | 0 | 0 | 1 |
因此,正如您在上面看到的那样,一个合作伙伴是任何供应商的所有者,其余合作伙伴是供应商的雇员。
我正在使用 Eloquent ORM,我已经为合作伙伴和供应商定义了模型。我想在供应商模型中添加一个 owner
方法,这样我就可以直接访问任何供应商对象的所有者。我想知道的是如何在我的模型定义中关联它。它是可行的还是我需要对我的数据库结构进行一些更改?
class Vendor extends Model{
/**
* Get all the users for this vendor
*/
public function users(){
$this->hasMany(Partner::class);
}
public function owner(){
// how do i relate one owner from partner model who has is_owner == 1
}
}
试试 where()
public function owner(){
$this->hasOne(Partner::class)->where('is_owner', true);
}
您可能必须在关系中指定外键。
与其定义新的关系,并导致重复代码和破坏 DRY 原则,不如利用 Eloquent 中开箱即用的 Local Scopes ORM.
Local scopes allow you to define common sets of constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all users that are considered "popular". To define a scope, simply prefix an Eloquent model method with scope.
Scopes should always return a query builder instance:
例如:- 在您的 Vendor 模型中定义一个函数 ScopeOwner(),例如:
public function scopeOwner($query)
{
return $query->where('is_owner', 1);
}
然后在您的控制器中使用它或按如下方式实现它:
$vendor->users()->owner()->get();
Once the scope has been defined, you may call the scope methods when querying the model. However, you do not need to include the scope prefix when calling the method. You can even chain calls to various scopes.
接下来,您甚至可以使用动态作用域来更好地重用代码。
Sometimes you may wish to define a scope that accepts parameters. To get started, just add your additional parameters to your scope. Scope parameters should be defined after the $query parameter:
public function scopeOfType($query, $type)
{
return $query->where('is_owner', $type);
}
并按如下方式消费:
$vendor->users()->ofType(0)->get();
更多信息,查看官方文档:Eloquent ORM Scopes