3 回答

TA貢獻1951條經驗 獲得超3個贊
我認為最直接的方法是使用數據透視模型基于數據透視表建立獨立關系:
class ShiftEmployee extends Pivot
{
protected $table='shift_employee';
}
現在是 Shift 模型中的新關系:
class Shift extends Model
{
public function shortlistedApplications()
{
return $this->hasMany(ShiftEmployee::class,'shift_id');
}
public function scopeWithShortlistedApplications($query)
{
$query->with('shortlistedApplications:shift_id,shortlisted');
}
}
現在這個新范圍將帶來你想要的數據

TA貢獻1890條經驗 獲得超9個贊
我認為你需要的是只加載shortlisted你的員工應用程序的屬性scopeWithApllications:
public function scopeWithApplications($query)
{
$query->with('applications.application:id,shortlisted');
}
這仍然會返回一個Application實例作為關系,但只會加載它的shortlisted屬性。然后,在檢索之后,您可以映射您的集合,以便將應用程序的屬性合并到您的員工(如果這真的很重要)。但就數據短缺而言,這可以解決問題。

TA貢獻1798條經驗 獲得超3個贊
在您的應用程序模型中使用 withPivot 方法。像這樣:
public function applications(){
return $this->belongsToMany('App\Application')
? ? ->withPivot('shortlisted')
? ? ->withTimestamps();}
- 3 回答
- 0 關注
- 188 瀏覽
添加回答
舉報