Lia97's avatar
Level 1

Crashes when using Union and Pagination

I have code

            $medias = DB::connection('oracle').....
               
	$materials = DB::connection('oracle').....
                 
	$union = $materials->union($medias);

	//pagination    
	$page = Request::get('page', 1);
	$paginate = 50;

	$offSet = ($page * $paginate) - $paginate;
	$itemsForCurrentPage = array_slice($union, $offSet, $paginate, true);
	$union = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($union), $paginate, $page);

	return view('ReceiveMedia.index', compact('union'));

When running it I get an error like this

Error : ErrorException (E_WARNING) array_slice() expects parameter 1 to be array, object given

I searched the solution, I get like this

	$union = $materials->union($medias)->get()->toArray();

But, it keeps me getting an error

Error : Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Maximum execution time of 60 seconds exceeded

I'm so frustrated about this. Please if you know a solution would be to tell me

0 likes
2 replies
MichalOravec's avatar

Add this you your AppServiceProvider

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') {
    $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

    return new LengthAwarePaginator($this->forPage($page, $perPage), $total ?: $this->count(), $perPage, $page, [
        'path' => LengthAwarePaginator::resolveCurrentPath(),
        'pageName' => $pageName,
    ]);
});

Then you can use paginate on the collection.

Lia97's avatar
Level 1

I've added it, and it still does not work and remains error. What is my code is correct like that?

Please or to participate in this conversation.