Reputation: 9183
I have created a Laravel Eloquent scopeFunction
but it returns std object even though I use
toArray()
method.
It is a conditional scopeFunction
, the first condition returns std::
object while the second
one works well and returns array. Can anyone see what is the problem?
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();
}
}
Upvotes: 0
Views: 481
Reputation: 626
Laravel does allow you to change how databases results are returned, i.e. their format.
From 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."
From the 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).
In your case, please see:
PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set
Hope that helps!
Upvotes: 0