midascodebreaker's avatar

Need Help in Pagination on Lesson Dedicated Query String Filtering

Hi Ive Followed Jeff Lesson, but i did more than he did such as filtering many to many relationship Product BelongsToMany Category and Category BelongsToMany Product

$products = Product::filter($filters)->published()->distinct()->get(['id', 'name', 'description', 'caption', 'price', 'image', 'slug', 'sku', 'thumbnail', 'options', 'rating_cache', 'rating_count', 'views']);

The Following are the Method in My ProductFilters

public function popular($order = 'desc')
    {
        return $this->builder->orderBy('views', $order);
    }
    /**
     * Filter by Category.
     *
     * @param  string $level
     * @return Builder
     */
    public function categories($categories = [])
    {

        if(!is_array($categories)){
            return $this;
        }

        return $this->builder
        ->join('category_product', 'category_product.product_id', '=', 'products.id')
        ->whereIn('category_product.category_id', $categories);
        
    }
    /**
     * Filter by No of Star Review.
     *
     * @param  string $order
     * @return Builder
     */
    public function starrating($order)
    {
        return $this->builder->where('rating_cache', '>=', $order);
    }
    /**
     * Filter by Product With Most No of Reviews
     *
     * @param  string $order
     * @return Builder
     */
    public function reviewcount($order = 'desc')
    {
      return $this->builder->orderBy('rating_count', $order);
    }
    /**
     * Filter by No of Results You Want to Get
     *
     * @param  string $order
     * @return Builder
     */
    public function take($count = null)
    {
        if(empty($count)){
            return $this;
        }
        if(is_numeric($count)){
            return $this->builder->limit($count);
        }
        return $this;
    }

Now With These Code i can Filter Exactly What i Want... But the Only Problem i Got Now is How Can i Paginate the Result

Coz if i add ->paginate(10) before ->get() method , the code is totally messed up and im receiving error how ever if i remove the get() method and add the paginate(10) method. My Result Shows Multiple , It shows the same product multiple times.

On Top Of That the Querry String Produce

{!! (new App\Pagination($products))->render() !!}

By Lavish Pagination package doesnt have all the querry string below which should be appended. On the pagination.

http://mysite.dev/products?categories%5B%5D=1&categories%5B%5D=3&popular=desc&starrating=0&reviewcount=desc&take=all

ive Seen Laracast Uses Has Such Filter as Mine but Dont Duplicate Result...

Calling @Jeffrey_Way

0 likes
2 replies
midascodebreaker's avatar

Ok i Manage To refine My Code and Got the result i wanted I Finally Have Been Able to have this kind of url

http://royalflushnetwork.dev/products?popular=desc&starrating=0&reviewcount=desc&take=all&page=1

I did Just Add an empty public function page in Productfilters

It Seems Like the Page Query String is Already Dedicated in Laravel.

Even if i Can Navigate Manually Using manual typing of url

the Return paginateurl needs to be configure for it to make it work

i still getting products?page=1

My next question if Someone Would be Kind enough to Dwell in my problem...

I know i can use the setPath('DESIRED URL')

How can i use it to return the previous query string ? such as popular , starrating, categories...

How can i dynamically return it every request?

Thanks

midascodebreaker's avatar
Level 17

Got it All Fixed by creating a private method and use appends() method of paginator class...

Please or to participate in this conversation.