Playing
22. The withAttributes relationship method

Transcript

00:00
Let's talk about the new withAttributes method that we can apply to relationships on our models. Now this is incredibly useful because it actually does two things. So let's go through these one by one and then we'll see that combined these can be really helpful in setting defaults and also scoping by certain attributes as well. Okay so to demonstrate this I've got a model with a post
00:24
relationship. Let's open this up very very straightforward. We just have a hasManyPosts and I've already created some over in the database here and we've got a featured column. So this is just a boolean column and you can see here I've got two featured and one not featured. At the moment in the browser I'm just dumping out all of the posts and of course we get a Laravel collection
00:47
of three items. Let's go ahead and create out a scope for this. So what we would do is I would typically come over here. I would duplicate something like this method. I'd say featured post and then you could either redefine hasMany or you could reference posts here and then typically what we would do is add a where clause onto here and all this means is it just makes it incredibly
01:13
straightforward to scope these by what is featured and what is not. So we're going to say we're featured true. Really really straightforward. You might have done something like this in the past and then of course when we come over here and we dump out featured posts that's only going to give us two. So you've probably done something like that in the past. Pretty straightforward and that just
01:32
gives you your code probably a lot more clarity. Now what has been added to Laravel is a with attributes. So let's go ahead and add this in method. What this allows us to do is we're not going to think of this as a complete direct replacement for where. That's not really the specific purpose of this but we're going to pass in an array and of course because it's an array
02:00
now we can pass in multiple attributes that we want to map values to and we're going to say featured and true. Now by doing that we're going to get exactly the same result. So bear with me let's give this a refresh and you can see sure enough we still get two here. Now the difference between using with attributes and where with attributes when we create a record via this relationship,
02:26
this featured posts relationship, which I'm actually going to change the name of here, this will pre-fill in featured to true. Now to demonstrate this let's go back over to here and say featured posts. I'm going to just temporarily comment this out and bring this back to how it was before. So let's go ahead and say where, which is perfectly acceptable still you don't need to
02:48
not do this, featured and true. So we know that over here that's going to give us two records but what about when we want to go ahead and create a featured post? Well we'd say user featured posts and we'd use the actual relationship here and we would say create, we'd fill in the title and stuff like this but I haven't actually added these to the model. Now
03:09
when we go ahead and create that featured post what has happened? Well over in the database a post has been created but it hasn't pre-filled featured. So let's go ahead and get rid of this from the database and let's go back over and let's switch this up for with attributes which will scope it by featured but it will also pre-fill in featured for us. So let's go back
03:34
over to where, we'll do exactly the same thing here without specifically passing this through and we now end up with another featured post. So if you have been using relationships like this where the whole purpose of it is to lean on an existing relationship but then scope it using a where clause, if it makes sense for you to then when you're creating through that relationship
03:59
pre-fill in the attributes then you can now do that with the new with attributes method and you can of course because this is an array pass in as many as you need here. So this is incredibly helpful and I will definitely be using this because I tend to define a lot of these kinds of relationship methods out.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!