In this video, Justin demonstrates how to write a one-to-many relationship in Laravel, where one entry in the database relates to multiple other records elsewhere in the database.
- [Instructor] A one to many relationship … is a relationship where one record in our database … relates to multiple other records in our database. … We have a few examples of this in our database. … One key place where this occurs … is actually with our RoomType to room. … You should have noticed by now … that we have one RoomType, … but it relates to multiple other rooms. … Let's fill that out now. … We'll open up our RoomType model at app, RoomType.php. … We'll add a new function to find the relationship … to all the rooms between a RoomType … and the related rooms for it. … We'll add, on line 14, a new function, … public function rooms. … Notice one of our differences with this function … is it's not pluralized. … This should make some sense. … In this case, … because rather than it returning a single instance … of the related model, … in our case it's going to return a collection … of the rooms that are associated with that RoomType. … On line 16 we'll define the relationship … as a hasMany relationship. …
- Exploring Laravel
- Setting up simple authentication
- Controllers and routes
- Create and index view
- Show, edit, and destroy views
- Writing scopes in Laravel Eloquent
- Creating one-to-one relationships
Skill Level Beginner
MVC Frameworks for Building PHP Web Applicationswith Drew Falkman2h 57m Intermediate
Ajax with PHP: Add Dynamic Content to Websiteswith Kevin Skoglund3h 37m Intermediate
What you should know1m 52s
2. Starting Your Project
3. The Create and Index Views
4. The Show, Edit, and Destroy Views
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.