در حال بارگذاری ...

Query Builder Returns Collection0:00

Okay, so next up, we're talking about the Query Builder, a pretty small change, but an important one to know. So now, the Query Builder will return a Collection rather than an array. Let me give you a little example here, and then we'll move on to the next episode. So you'll notice that behind the scenes, I've specified that we want an SQLite database, and that of course will be the same for both projects. Okay, so take a look. We're going to go to the command line for Laravel 5.3, php artisan tinker. Actually, a quick little note, create a little alias for yourself called Tinker. This is one I don't know about you, but I use it all the time.

Creating Users in Tinker0:28

Actually, a quick little note, create a little alias for yourself called Tinker. This is one I don't know about you, but I use it all the time. So it saves quite a bit of time. Okay, so anyways, we're going to set up a factory for a User, and I don't know, we will create two of them. All right, just using the standard model factories. All right, so that's Laravel 5.3. In Laravel 5.2, I'm going to do the exact same thing. So a factory for a User, and I want two of them. All right, so take a look.

Laravel 5.2 Returns Array0:53

So a factory for a User, and I want two of them. All right, so take a look. If we use the query builder, we could say, using the users table, naturally we have the User model, but just assuming you have to reference the DB facade, if you run a essentially select * , what you're going to get in response is an array. So that means, for example, you can't do any collection-y type stuff. Yeah, you can't do that stuff because it's an array. So best case scenario, yeah, you can use indexes and stuff like that, but that's about it.

Laravel 5.3 Returns Collection1:23

So best case scenario, yeah, you can use indexes and stuff like that, but that's about it. So if we switch over to Laravel 5.3 though, well, if we try to run the exact same command, what you'll see is that you now get an instance of Illuminate\Support\Collection. So if we scroll up, yeah, you have a full collection class. And if you think about it, up until this point, it was kind of inconsistent, because anything using Eloquent would return a collection, but using the query builder would return an array, and it just gets kind of confusing. So the important thing for

Converting Back to Array1:50

using the query builder would return an array, and it just gets kind of confusing. So the important thing for you to know is that if you don't want that collection response, then you're going to want to update any query builder calls to reference the all method at the end. And that's just going to give you the underlying array of items. But otherwise, yeah, what's cool is now for any call you have access to the full variety of collection methods. For example, maybe give me the first one where the User's email address, kind of a stupid example, actually.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟