matthallett's avatar

Pagination with a custom database and implementing toString()

I'm connecting to a database via an ODBC driver. The database uses a slightly different query language (WX2, which is based on Oracle) which doesn't really work with so what I've done is create my own class, service provider and facade to deal with the queries (which I pass in raw format rather than using Eloquent). As I'm dealing with large datasets, I want to paginate.

I had things working quite well initially, mimicking the normal paginate array. However to loop through the items, I had to say "foreach $model['data'] as $result" etc and I can't do things like $model->first();

So I took a look at the Illuminate code. I took this from BuildsQueries:

return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
            'items', 'total', 'perPage', 'currentPage', 'options'
        ));

Which returns the pagination array fine and allows me to loop without the ['data'] bit. But when I do $model->first(), I get an error saying the response must be an object implementing __toString(). If I DD($model->first()) it shows the result so that side of it is working, it just can't display the end result.

Is there a simple way for me to do this?

0 likes
1 reply
mushood's avatar

I might be out of depth here, but hope this helps.

While queries like ->get() returns a collection of object, ->first() returns an instance of the object itself.

I would say the easy way would be to have your model implement a __toString() method.

Please or to participate in this conversation.