Jump to content

elequent ,returns Model class or Collection class?


ali_254
 Share

Recommended Posts

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?!!

Link to comment
Share on other sites

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. 

  • Like 1
  • Great Answer 1
Link to comment
Share on other sites

Posted (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 by ali_254
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.