gpt4 book ai didi

php - Laravel 5 Eloquent 模型结果

转载 作者:搜寻专家 更新时间:2023-10-30 22:07:53 25 4
gpt4 key购买 nike

我正在使用 Eloquent 模型从数据库中检索数据。我的查询

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

据我所知,$tests_details 将返回对象数组(结果),但是当我回显 is_array($tests_details) 时,它返回 false,这意味着它不是数组但是当我回显 count($test_details) 时,它显示了正确的计数。请参见下面的代码

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

它为 is_object($td) 返回 1。当我使用 print_r 函数打印 $tests_details 时,下面是我得到的输出 enter image description here

什么时候回显 $tests_details[0]->edate 它显示正确的输出并且一切正常。但我想知道为什么 eloquent 以图像中显示的格式而不是普通对象返回数据。我是 laravel 的新手,目前使用的是 laravel 5.0,任何解释都是值得理解的。

最佳答案

Previous_Mocks 模型返回的数据是一个集合对象。

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.

但是,集合比数组强大得多,并且公开了可以使用直观界面链接的各种映射/归约操作。

Eloquent Collection Reference

关于php - Laravel 5 Eloquent 模型结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43466448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com