0

Here is my code

Controller

$query = "SELECT `h`.*,".$countquery.",".$minquery." FROM `abserve_hotels` as `h`";
$aReslts = $this->model->getData($query,$sqlCond);

$this->data['Rooms'] =!empty($aReslts) ? $aReslts : '';

return view('hotel.list', $this->data);

Here,$sqlCond returns several result set while passing Ajax and for post action

View

<?php
if(($Rooms)){
?>
@foreach($Rooms as $room)
{{ $room->id }}
@endforeach

<?php
}
?>

I tried several ways to set the pagination methods..But it din't work for me..

How should I make pagination for this..Could someone help me!!..

Thanks in advance

3 Answers 3

2

You can create a paginator manually by using

$pagination = Paginator::make($this->data['Rooms'], count($this->data['Rooms']), 5);

To display links use echo $pagination->links(); Or {{ $pagination->links() }}

Sign up to request clarification or add additional context in comments.

Comments

2

Use this way .... very easy

public function items(Request $request)
{
    $items = [
        'item1',
        'item2',
        'item3',
        'item4',
        'item5',
        'item6',
        'item7',
        'item8',
        'item9',
        'item10'
        ];

    // Get current page form url e.x. &page=1
    $currentPage = LengthAwarePaginator::resolveCurrentPage();

    // Create a new Laravel collection from the array data
    $itemCollection = collect($items);

    // Define how many items we want to be visible in each page
    $perPage = 1;

    // Slice the collection to get the items to display in current page
    $currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all();

    // Create our paginator and pass it to the view
    $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage);

    // set url path for generated links
    $paginatedItems->setPath($request->url());

    return view('items_view', ['items' => $paginatedItems]);
}

1 Comment

Thanks. It worked for me!! Also in blade file I added {{ $paginatedItems->links() }} to display pagination Links.
0
public function controllerFunction($page=1)
{
   $limit = 10;
   $start = ($page-1)*$limit;
   $query = "SELECT `h`.*,".$countquery.",".$minquery." FROM `abserve_hotels` as `h`";
$aReslts = $this->model->getData($query,$sqlCond)->skip($start)->take($limit);

$this->data['Rooms'] =!empty($aReslts) ? $aReslts : '';

return view('hotel.list', $this->data);
}

This will give you paginated content. You can add pagination on your view via laravel pagination or using a custom code.

3 Comments

Using this code in controller and adding ` echo $aHotelRooms->render(); ` in view may set pagination!!...
And in my controller function I already pass a Request $request how should I give the $page=1 by using , !!...
When use echo $Rooms->render() I'm always getting 1 FatalErrorException in 3d4c6f287876462533e51d881f81767c line 45: Call to a member function render() on array what should I do for it.. : (

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.