我想达到的目标。I处理4张表格:
branch_name)
规则
branch
管理。
下面的是我的分支模型代码
<?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);
}
}下面的是我的人员模型代码
<?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的代码
<?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);
}
}修补者的结果:
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?
发布于 2022-04-02 22:00:29
员工与分支机构的关系是一对多的关系,有一个专门的员工实体,可以作为分支机构的负责人。
branch_name)
)
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);
}
}添加注释与此结构的任何问题。我会在回复中编辑。
让所有的工作人员从一个分支机构得到使用
$branch->staff它将包括部门主任工作人员(您可以检查旗标$staffMember->is_head_branch == 1)
要获得所有BranchHeads及其关联分支或相反的方式,请执行以下操作
$headBranchs = Staff::where('is_head_branch', 1)->with('branch')->get();
//or
$branchs = Branch::with('branchhead')->get();https://stackoverflow.com/questions/71720815
复制相似问题