Laravel 5.8 – From The Ground Up
Lesson 46 of 52 · Laravel 5.8 Tutorial From Scratch – e46 – Eloquent Relationships One To Many (hasMany, BelongsTo) · Education
Laravel 5.8 Tutorial From Scratch – e46 – Eloquent Relationships One To Many (hasMany, BelongsTo)
let's take a look at a one-to-many relationship in laravel this is probably the most common of the relationships and it basically states that there is one
owner and that owner has many of the secondary property and what I mean by that is the example we're gonna be working on today today we're gonna be working on a user has many posts so a
user can have an unlimited amount of posts but a post belongs to a user so I have a fresh level installation here so let's start working from scratch let's make a new model PHP artisan make model
for a post and go ahead and give me a migration for that there we go we're gonna keep this very simple for the example let's go to the post and let's just set guard it to an empty array that
way we disable mass assignment and let's go to the create post table and let's just keep it very simple this table is gonna have a string for a title in just a text for a body and of course as
always it's gonna need an unsigned big integer for a user ID because of course each post belongs to a user so we need that here to reference the user ID now we are going to be using labels user
table that it comes with without changing anything at all alright so we have that we have that now let's go ahead and migrate our database so PHP artisan migrate and there we go alright
so that is ready to go for us so let's set up these relationships let me pull up the post model and let's add a new public function for user so remember a post has one user so return this belongs
to app user class perfect now we need to go to the user and do the same thing so let's go over here to user and at the bottom let's add a new public function for posts notice that this is plural not
singular because remember a user has many posts so it needs the plural posts and in here we'll say return the has many what does it have many of well it has many a post and that's it our
relationship is good to go from there on out so a user has many posts and a post belongs to a user perfect so let's jump into the web routes just to have a quick playground here and let's whip up a post
so we can say our post is going to equal a new app post and we want our post to have of course we know it needs a title title here and we know that it needs a body so we'll just say body here so at
this stage we have a post but we haven't saved it to the database let me die in dump but just so that you see what we have let's jump to the browser and there we are we have a post and we have two
things but notice that it is not persisted we need to persist it and so to persist it we actually need to make a user so let's make a user here at the top we'll
say our user equals let's use our factory and say give me an app user class create one of those forests so at this stage you could do several things right so the long form of this is that
we would actually pass in a user ID and we can say well the user ID is gonna be the user ID that makes sense and then we can call post safe so this is obviously the long form let's see
what happens hit refresh it says that the body was not found let's check out what happened create post table ah I misspelled that body not bad
alright perfect let's try it again PHP artisan migrate fresh there we go let's try there again there we go now it's working so we can tell that it's saved because we actually have an ID but
we're having to pass in a user ID so let's jump back to the PHP storm and let's check out the short form of this so we can get rid of this altogether what we could do is use the relationship
on our user model so that would look something like user and then we're going to call that posts and then we're gonna say create and to this create method is what we're
going to pass in this array right here so let's cut that out let's put it in here and now we no longer need any of that and then we can simply say grab the user and give me their posts that way we
get all the posts together and if we hit refresh we have another one so there we go so that is working exactly the same way you see that the user ID is being set for us even though we are not
actually telling laravel how to do that so that's pretty cool so we have that now but what about updating how do we update this post well there's several ways that you can do that for example
what if I want to take my post and then I'm gonna say give me the first one that way I have a post instance and then I want to override the title let me actually jump this out of the Dynamo
so the title is gonna be new title but then I want to save all of that so we can actually say user push what that's gonna do is save the changes to our post but notice that I'm calling push on user
not on post let's see what happens now hit refresh actually we don't get anything we do need to die in dump let's die and dump something else actually know what let's return our user post
that will give us a JSON array refresh and sure enough we see that we have new title here so that's how you would update a relationship just to drive the point even further let's go ahead and
change the body as well new better body how about that so now we're gonna push those changes and then we're gonna fetch that let's see refresh so now we have new better body and new title so that's
how you would update the post through the user relationship now of course in the long form of this just to show you the difference I'm gonna get rid of that and I'm going to say alright so my post
is gonna be user post and then I could say post title equals new 1 2 3 and then we can change the body to body 1 2 3 and then of course I can call post save those changes for me all right let's try
that now I'm sorry user post this needs to be first that's gonna return a collection and then we have first all right let's try it again there we go
so we have new one two three and body one two three but of course that's not ideal because imagine that you had two or three different relationships and you're trying to save everything at the
same time so of course the nicer way of doing this let me return back to what we had before is to do it this way you're going to make all of your changes and then on your parent model you're gonna
call the push and push will automatically push all of those changes over into the relationship without you having to go in individually and do one at a time so there we go that is a
one-to-many relationship in level now there are a couple of more methods that you can call like save many or create many that will help you create several posts in one single shot so I suggest
you go check out the one-to-many relationship in the documentation if you're in need of something like that but for now this will cover the basics of a one-to-many relationship in laravel
Eloquent offers a simple way to declare database relationships. In this episode, we are setting a one to many relationship between a user and posts.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Code for this Lesson
https://github.com/coderstape/laravel-relationships/compare/one-to-many
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!