在我的 Laravel 8 应用程序中,我有以下模型。
公司
protected $fillable = [
'id',
'name',
'website',
'company_logo',
'registration_number',
'date_established',
'address',
];
员工
protected $fillable = [
'id',
'company_id',
'user_id',
'first_name',
'last_name',
'other_name',
'gender',
];
public function company()
{
return $this->belongsTo(Company::class,'company_id','id');
}
public function user()
{
return $this->belongsTo(User::class,'user_id','id');
}
我开始了这个,但在路上卡住了。我想根据登录的用户选择公司详细信息。公司表应该是主表:
public function getMyCompany()
{
try {
$userId = Auth::user()->id;
$employeeId = Employee::where('user_id', $userId)->first();
$company = Company::...;
return $this->success('Company successfully Retrieved.', [
'company' => $company
]);
} catch (\Exception $e) {
Log::error($e);
return $this->error($e->getMessage(), $e->getCode());
}
}
我如何使用以下方法实现这一目标(选择公司的所有详细信息):
$company = Company::...;
制作主模型