我创建了一个Laravel Eloquent scopeFunction
,但它返回std对象,即使我使用
toArray()
法
是有条件的scopeFunction
,第一个条件返回std::
对象,第二个
1可以很好地工作并返回数组。有人知道问题出在哪里吗?
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();
}
}
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手册:
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
希望有帮助!