首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用Laravel 9通过hasOne的方法

利用Laravel 9通过hasOne的方法
EN

Stack Overflow用户
提问于 2022-04-02 20:04:27
回答 1查看 527关注 0票数 1

我想达到的目标。I处理4张表格:

branch_name)

  • staff (id(PK),staff_name)

  • branch_heads(staff_id(FK -staff,branch_id(FK -branch))

规则

branch

  • branch属于
  1. Staff,有多个员工
  2. ,每个分支机构由一个员工

管理。

下面的是我的分支模型代码

代码语言:javascript
复制
<?php

namespace App\Models\CompanyStructure;

use App\Models\StaffManagement\Staff;
use Illuminate\Database\Eloquent\Model;
use App\Models\CompanyStructure\BranchHead;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Branch extends Model
{
    use HasFactory;
    protected $fillable = [
        'branch_name',
    ];

    public function staff()
    {
        return $this->hasMany(Staff::class);
    }

    public function branchhead()
    {
        return $this->hasOne(BranchHead::class);
    }
}

下面的是我的人员模型代码

代码语言:javascript
复制
<?php

namespace App\Models\StaffManagement;

use App\Models\CompanyStructure\Branch;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Staff extends Model
{
    use HasFactory;
    protected $table = 'staff';
    protected $fillable = [
        'staff_name',
    ];

    public function branch()
    {
        return $this->belongsTo(Branch::class);
    }

}

下面的是BranchHead Model的代码

代码语言:javascript
复制
<?php

namespace App\Models\CompanyStructure;

use App\Models\StaffManagement\Staff;
use App\Models\CompanyStructure\Branch;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class BranchHead extends Model
{
    use HasFactory;

    protected $fillable = [
        'staff_id',
        'branch_id',
    ];

    public function staff()
    {
        return $this->hasOne(Staff::class);
    }

    public function branch()
    {
        return $this->hasOne(Branch::class);
    }

}

修补者的结果:

代码语言:javascript
复制
Branch::find(1)->branchhead -- gives me the results of the staff_id that manages a particular branch_id. But can't go beyond that level to get which staff_name that manages which branch.

我使用的是Laravel 9和PHP8.1

我怎样才能得到staff_names?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-02 22:00:29

员工与分支机构的关系是一对多的关系,有一个专门的员工实体,可以作为分支机构的负责人。

branch_name)

  • staff (id(PK),branch_id(FK -branch),staff_name,is_head_branch)

)

代码语言:javascript
复制
class Branch extends Model
{
    use HasFactory;
    protected $fillable = [
        'branch_name',
    ];

    public function staff()
    {
        return $this->hasMany(Staff::class);
    }

    public function branchhead()
    {
        return $this->hasOne(staff::class)->where('is_head_branch',1);
    }
}

添加注释与此结构的任何问题。我会在回复中编辑。

让所有的工作人员从一个分支机构得到使用

代码语言:javascript
复制
$branch->staff

它将包括部门主任工作人员(您可以检查旗标$staffMember->is_head_branch == 1)

要获得所有BranchHeads及其关联分支或相反的方式,请执行以下操作

代码语言:javascript
复制
$headBranchs = Staff::where('is_head_branch', 1)->with('branch')->get();
//or
$branchs = Branch::with('branchhead')->get();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71720815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档