Last active
August 30, 2017 06:45
-
-
Save tajulasri/63cd1e6357c2e5dfee5c9d4a76805040 to your computer and use it in GitHub Desktop.
problem nub saya
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class User extends Model { | |
public function applications() | |
{ | |
return $this->belongsToMany(Application::class,'user_applications')->withPivot('application_status_id'); | |
} | |
} | |
class Application extends Model { | |
public function user() | |
{ | |
return $this->belongsToMany(User::class,'user_applications')->withPivot('application_status_id'); | |
} | |
} | |
User::with('applications')->whereApproverId(1)->get(); //return Collection() | |
//solution sekarang tapi output bukan macam dibawah sekadar collections of applications | |
$userApplications = collect(); | |
$toCollection = User::whereApproverId(1) | |
->each(function ($user) use ($userApplications) { | |
$user->application() | |
->each(function ($application) use ($userApplications) { | |
//application nak load sekali ngn relations yang ade sekali kalau logik | |
$userApplications->push(new Application($application->toArray())); //push attributes | |
}); | |
}); | |
Application model akan ade dalam setiap relations bg setiap item dalam collection la kan? | |
so user tu hanya id yang sama..ade care tak untuk flatkan applications tu supaya return die same mcm hasMany | |
{ | |
"approver_id": 1, | |
"applications": [ | |
{ | |
"pivot": { | |
} | |
}, | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment