Laravel cache::remember is returing object as an array

Viewed 2097

Laravel Cache::remember is returning a LengthAwarePaginator object as an array.

function getNotifications( $userID ) {
    $values = Cache::remember('cache-key', 10, function() {

        $query = DB::table( 'user_notifications' )
                ->leftJoin( 'notifications', 'user_notifications.notification_id', '=', 'notifications.id' )
                ->where( 'user_notifications.user_id', $userID )
                ->select( 'notifications.*' )
                ->orderBy('created_at', 'DESC')
                ->paginate(5);

        return $query;

    });

    return $values;
}

If I dd($query) before returning from Cache closure, it's returning the following object, that accepts $value->links() to display pagination. LengthAwarePaginator

But whenever the Cache is storing $query into $values it's returning values as an array: Array of values

I tried commenting out the unserialize-block:

/*foreach( $values as $key => $value ) :
    $values[$key]->meta = self::maybeUnserialize($value->meta);
endforeach;*/

and confirmed that, that's not the cause.

I also tried, but failed:

$values = collect($values);

With multiple check and cross-check I am confirming that, the issue is the Cache::remember.

How can I force the Cache::remember return things as it is? So that I can let $object->links() work for me.

The actual code can be found here.

1 Answers
Related