teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.1k stars 144 forks source link

Pagination with constraints #204

Closed mefenlon closed 5 years ago

mefenlon commented 6 years ago

I have a few scopes that I am using for constraints. When they are not triggered, the pagination functions fine. But If a constraint is uses, the pagination acts as if the full set (527) is there even though the collection contains the correct number results (6)

/**
 * Scope a query to only include illustrations with illustrators of a given ids.
 *
 * @param \Illuminate\Database\Eloquent\Builder $query
 * @param array $ids
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeWhereHasIllustratorsWithIDs($query, $ids)
{
    if(!empty($ids)) {
        return $query->whereHas('illustrators', function ($query) use ($ids) {
            $query->whereIn('name_id', $ids);
        });
    }
    return $query;
}

/**
 * Scope a query to only include illustrations with indextags of a given ids.
 *
 * @param \Illuminate\Database\Eloquent\Builder $query
 * @param string $ids
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeWhereHasArticleYears($query, $yearStart, $yearEnd)
{

    if(!empty($yearStart)) {
        //pad years
        $yearStart = str_pad($yearStart,8,'0');
        $yearEnd = str_pad($yearEnd,8,'9');
        return $query->whereHas('article', function ($query) use ($yearStart, $yearEnd) {
            $query->
                where([
                    ['date_iso', '>=', $yearStart],
                    ['date_iso', '<=', $yearEnd],
                ]);
        });
    }
    return $query;
}

        //Controller
        $constraints = new Illustration;
        if(isset($request->illustrators))
            $constraints = $constraints->whereHasIllustratorsWithIDs($request->illustrators);
        $constraints = $constraints->whereHasArticleYears($year_start, $year_end);

        $illustrations = Illustration::
            search($request->search)->constrain($constraints)->paginate(25);

        dd($illustrations);

//Output with no constraints LengthAwarePaginator {#623 ▼

total: 527

lastPage: 22

items: Collection {#677 ▼

#**items: array:25** [▼
  0 => Illustration {#466 ▶}
  1 => Illustration {#704 ▶}
  2 => Illustration {#479 ▶}
  3 => Illustration {#465 ▶}
  4 => Illustration {#477 ▶}
  5 => Illustration {#467 ▶}
  6 => Illustration {#468 ▶}
  7 => Illustration {#657 ▶}
  8 => Illustration {#473 ▶}
  9 => Illustration {#617 ▶}
  10 => Illustration {#618 ▶}
  11 => Illustration {#475 ▶}
  12 => Illustration {#476 ▶}
  13 => Illustration {#471 ▶}
  14 => Illustration {#474 ▶}
  15 => Illustration {#659 ▶}
  16 => Illustration {#660 ▶}
  17 => Illustration {#472 ▶}
  18 => Illustration {#469 ▶}
  19 => Illustration {#470 ▶}
  20 => Illustration {#702 ▶}
  21 => Illustration {#701 ▶}
  22 => Illustration {#478 ▶}
  23 => Illustration {#480 ▶}
  24 => Illustration {#703 ▶}
]

}

perPage: 25

currentPage: 1

path: "http://localhost:8000/illustrations"

query: array:1 [▼

"query" => "meyerbeer caricature"

]

fragment: null

pageName: "page"

}

//With constraint ( LengthAwarePaginator {#623 ▼

total: 527

lastPage: 22

items: Collection {#677 ▼

#**items: array:6** [▼
  0 => Illustration {#494 ▶}
  1 => Illustration {#493 ▶}
  2 => Illustration {#495 ▶}
  3 => Illustration {#496 ▶}
  4 => Illustration {#491 ▶}
  5 => Illustration {#498 ▶}
]

}

perPage: 25

currentPage: 1

path: "http://localhost:8000/illustrations"

query: array:1 [▼

"query" => "meyerbeer caricature"

]

fragment: null

pageName: "page"

}

surgiie commented 6 years ago

Have you figured this out? Im having similiar issues.

mefenlon commented 6 years ago

Looks like a known issue https://github.com/teamtnt/laravel-scout-tntsearch-driver/issues/142 I am going to attempt to add pagination to the model as suggested in the above thread.

surgiie commented 6 years ago

@mefenlon Thanks for some reason though adding the paginate macro was still returning unexpected results. I got around it by returning the builders results and creating a new collection from those results and paginating that instead:

See my comment:

#142 comment

mefenlon commented 6 years ago

I ended up with something similar.

This would be much clearer if the README was correct. $department comes out of nowhere

    //$department?
    $paginator` = $department->paginate(10);
    $posts` = $paginator->getCollection();
    // return posts