Hi,
Sorry for noob question.
I have a controller named Project, worknotes and attachments
The relationship is project has many worknotes and each worknotes can have many attachments
i am able to get the the worknotes for each projects using in project controller
$project = Projects::with("worknotes")->findOrFail($id);
in project model
public function project_worknotes(){
return $this->hasMany("App\models\worknotes");
}
.....
my question is, how can i get the relationship for worknotes and attachments
do i have to retrieve all the values first then loop on each?
Please advise
Thanks..
Your argument inside with
needs to match the method name for your relationship. In other words, if you do this:
$project = Projects::with("worknotes")->findOrFail($id);
Then, in order to eager load the relationships, your relationship's method's name needs to match:
// Note that I named the function "worknotes()", not "project_worknotes()".
public function worknotes(){
return $this->hasMany("App\models\worknotes");
}
Both have to be called either "worknotes" or both have to be called "project_worknotes". Choose one or the other.
Next, if you want to get nested relationships, you can do that with the dot notation.
$project = Projects::with("worknotes", "worknotes.attachments")->findOrFail($id);
Hi thomastkim,
Thanks! its working. The dot notation is what i'm missing.
Thanks!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community