prudhvi259
prudhvi259

Reputation: 547

Laravel 5 Eloquent Model Results

I am using eloquent model to retrieve data from database.My query

$tests_details = Previous_Mocks::where($data)->orderBy('sno', 'desc')->get();

As of my knowledge $tests_details will return array of objects(results) but when i echo is_array($tests_details) it is returning false which means it's not an array but when i echo count($test_details) it is showing the correct count.See below code

foreach ($tests_details as $td)
                {
                    echo is_object($td);
                    echo "<br/>";
                }

it is returing 1 for is_object($td). When i print $tests_details using print_r function below is the output i am getting enter image description here

and when is echo $tests_details[0]->edate it is showing proper output and everything working fine. But i want to know why the eloquent returned the data in the format shown in image instead of normal objects. I am new to laravel and currently using laravel 5.0 any explanation is appreciable.

Upvotes: 1

Views: 1098

Answers (1)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

The data return by Previous_Mocks model is a collection object.

All multi-result sets returned by Eloquent are instances of the Illuminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship. The Eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models.

However, collections are much more powerful than arrays and expose a variety of map / reduce operations that may be chained using an intuitive interface.

Eloquent Collection Reference

Upvotes: 3

Related Questions