From the course: Laravel Essential Training

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Solution: Define a new relationship and use it

Solution: Define a new relationship and use it - Laravel Tutorial

From the course: Laravel Essential Training

Solution: Define a new relationship and use it

The relation between note and notebook is very similar to that of note and user. It's a many-to-one relationship. A notebook has many notes and the note belongs to one notebook. So first, let's define the hasMany relationship in the notebook model. Public function notes. This will be plural because a notebook can have many notes. So return $this->hasMany(Note::). Okay, you might have to import the Note::class, app models note. Yes. Close this. Right. Now let's define the inverse relationship in the note model which would be public function notebook. This is singular because it's our single notebook. And you can copy this, return $this->belongsTo(Notebook::class). Okay. You might have to import if it's not imported. Great. Now go to the browser. And here, we have already added this note into the travel notebook. Let's save it once again. So what we need is we need to display the notebook name here right at the top. So let's open the show view of notes. And right here after the success…

Contents