我已经创建了一个Laravel Eloquent scopeFunction
但它返回了std对象,即使我使用
toArray()
方法。
这是一个条件scopeFunction
,第一个条件返回std::
个对象,而第二个
一个很好用并返回数组。任何人都可以看到问题是什么?
function scopeGetList($query, $firstId, $limit, $catid = 'all')
{
$cats = NULL;
if($catid != NULL && $catid != 'all')
{
$cats = CategoryList::where("cat_id", "=", $catid)->get()->toArray();
$res = NULL;
foreach($cats as $cat)
{
$res[] = $query->where("id", "=", $cat['item_id'])->take($limit)->skip($firstId)->orderby("id")->get()->toArray();
}
return $res;
}
else
{
return $query->take($limit)->skip($firstId)->orderby("id")->get()->toArray();
}
}
答案 0 :(得分:0)
Laravel允许您更改数据库结果的返回方式,即格式。
来自api / config / database.php:
"By default, database results will be returned as instances of the PHP
stdClass object; however, you may desire to retrieve records in an
array format for simplicity. Here you can tweak the fetch style."
来自PHP manual:
fetch_style
Controls how the next row will be returned to the caller. This value must be
one of PDO::FETCH_* constants, defaulting to value of PDO::ATTR_DEFAULT_FETCH_MODE
(which defaults to PDO::FETCH_BOTH).
在您的情况下,请参阅:
PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set
希望有所帮助!