ali_254 Posted April 4, 2022 Share Posted April 4, 2022 hi... hello everybody... I create model and wrote this code in controller (my model name in Category): $category=Category::all(); dd($category); When I use "all" method , This returns the result: Illuminate\Database\Eloquent\Collection {#1024 ▼ #items: array:3 [▶] #escapeWhenCastingToString: false } In fact, it is the output of the Eloquent/collection class. But, when I use "first" method: $category=Category::first(); dd($category); Returns the Models\Category class: App\Models\Category {#1022 ▶} According to Laravel documents: Quote As we have seen, Eloquent methods like all and get retrieve multiple records from the database. However, these methods don't return a plain PHP array. Instead, an instance of Illuminate\Database\Eloquent\Collection is returned. Our output must be from Eloquent/collection class ... But, when I use "first" method , Returns the Models\Category class. what is the reason?!! Quote Link to comment https://forums.phpfreaks.com/topic/314663-elequent-returns-model-class-or-collection-class/ Share on other sites More sharing options...
kicken Posted April 5, 2022 Share Posted April 5, 2022 1 hour ago, ali_254 said: But, when I use "first" method , Returns the Models\Category class. what is the reason?!! First only returns a single item, so there's no point in putting it in a collection. The collection is for methods that might return several items. 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/314663-elequent-returns-model-class-or-collection-class/#findComment-1594875 Share on other sites More sharing options...
ali_254 Posted April 5, 2022 Author Share Posted April 5, 2022 (edited) 7 hours ago, kicken said: First only returns a single item, so there's no point in putting it in a collection. The collection is for methods that might return several items. thanks... collection class in laravel , Uses standard php functions? (Functions of working with arrays in php) Or does it use other "mechanisms"? Edited April 5, 2022 by ali_254 Quote Link to comment https://forums.phpfreaks.com/topic/314663-elequent-returns-model-class-or-collection-class/#findComment-1594885 Share on other sites More sharing options...
gizmola Posted April 5, 2022 Share Posted April 5, 2022 It's a class that can be iterated with foreach. It is not an array, but it has many eloquent specific capabilities you can read about here: https://laravel.com/docs/9.x/eloquent-collections. For example it has a method that will return all the primary key values of all the models in the collection. Keep in mind that it extends the Laravel collection base class, which has many more methods that might be useful depending on what you want to do. See https://laravel.com/docs/9.x/collections 1 Quote Link to comment https://forums.phpfreaks.com/topic/314663-elequent-returns-model-class-or-collection-class/#findComment-1595041 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.