3 回答

TA貢獻1815條經驗 獲得超10個贊
您可以通過將變量傳遞給預先加載和更新您的關系結構來實現這一點。試試這個
User 模型:
class User extends Authenticatable
{
///
public function obtainLoggedOnCompany()
{
return $this->belongsToMany('App\Company', 'user_company', 'user_id', 'company_id');
}
}
Company 模型:
class Company extends Model
{
///
public function profile()
{
return $this->hasOne('App\Profile', 'company_id');
}
}
Example 一個用例:
$user_id = 1;
$user = User::where('id', $user_id)->with(['company' => function($query) use ($user_id) {
return $query->with(['profile' => function($query2) use ($user_id) {
return $query2->where('user_id', $user_id);
}]);
}])->first();
// $user variable contains user details, company details, and profile

TA貢獻1946條經驗 獲得超3個贊
實際上,這句話完美無缺?。?/p>
public function obtainSelectedProfile()
{
$SelectedCompany= $this->obtainLoggedOnCompany->first();
return $this->hasMany('app\Profile','user_id')->where('company_id', '=',
$SelectedCompany->company_id);
}
在我看來,我的錯誤在別處!
盡管我認為在將數據傳遞給視圖之前在控制器上準備數據是更好的做法,因為每次我想從視圖中的用戶配置文件中獲取數據時,我都必須查詢服務器。
- 3 回答
- 0 關注
- 206 瀏覽
添加回答
舉報