Laravel 5.7.* All Model Relations Explained (Silent as the Dead)
Laravel 5.7.* All Model Relations Explained
a detailed discussion of mvc, Composer, Migrations, Database Relationships, Resource controllers and faker, advanced crud, One to One, One to Many, Many to Many, Has Many through, and polymorphic relationships in laravel 5.7.*
About the Book
Any awesome Laravel application greatly depends on Model relations. How they work, what are the magical methods behind them? Explained lucidly with examples.
You will learn all Model relations and how you can use them to build a complicated web application.
Apparently, this contents management application does not look very complicated, however, the relations are quite tangled and we will try to understand these relations through Laravel’s Model relations and Eloquent ORM; at the end, we will also learn how resourceful controllers play vital roles in such model relations.
How an article is related to a particular user and how from the article table we can fetch the data of that particular user or writer? An article uses many tags, but how we can access those tags from that article itself? An article may have many comments. Who has written those comments? Of course users. And these users have their own profiles which tell us about them, including name, email, city, and a short biography. Each profile page may have multiple comments written by other users about that profile-holder user. So we have comments table for both – articles and profiles. Can we use one single comment table to serve them both? Yes, we can do! A polymorphic relationship allows us to do that.
These questions are all very tricky, however, laravel model relations, Eloquent ORM, and resourceful controllers has solved these problems quite easily. As we progress we will learn one-to-one, one-to-many, many-to-many, has-many-through, and polymorphic relationships with concrete examples. By the way, we will also learn some major tricks of Laravel (like resource controller and views) that makes your development experience more pleasant.
Finally, what we will see in this application? A user have many articles. When you open the application, you will find the series of article titles. Each title will show who is the writer of it. If you click on each title, you can read the full article. If you click on the user name, you will see the user’s profile and all the articles he/she has written. The same rule is applied for the tags. Each tag may belong to many articles. Again any article may belong to many tags. So each article shows many tags. In the user profile we will see which tags he/she has used often and so on. In addition to that, we have comments on both pages, with a link to the users who have written them.
All together, it’s an interconnected contents management application that handles many complex queries effortlessly.
Table of Contents
Who should read this book?
What you will Learn from this Book and The Application we are going to build
1. What is MVC Pattern
2. How MVC Pattern Works
3. Composer and Laravel
3.1 Composer’s role in Effective Patterns
3.2 Composer and Separation of Concerns
3.3 Laravel Homestead, Virtual Box and Vagrant
3.4 Installing Virtual Box and Vagrant
3.5 Installing Homestead Vagrant Box
3.6 Homestead installation and configuration
4. How to start our Application
5. How Migrations work in Laravel
5.1 Model Relations for Application
5.2 Controllers and Views
5.3 Creating Views to show Relationships
5.4 One To One
5.5 One to Many
5.6 Dependency Injection and Separation of Concerns
5.7 Many to Many
5.8 Has Many Through
5.9 Polymorphic Relations
6. Resource Controllers and Eloquent Magic
6.1 Model and Controller
Where to go from here
Last but not the Least
Supporting Indigenous Peoples. Protecting the Amazon.http://amazonwatch.org
Amazon Watch is a nonprofit organization founded in 1996 to protect the rainforest and advance the rights of indigenous peoples in the Amazon Basin. We partner with indigenous and environmental organizations in campaigns for human rights, corporate accountability and the preservation of the Amazon's ecological systems.
Other books by this author
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $12 million USDwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them