简体   繁体   中英

Laravel pagination not working with join where condtion

I am using laravel pagination for list. There is two tables lets say table1 & table2 .

table2 id is primary key & used as foreign key in table1 t2_id

I want to get all unique t2_id but need to check is_deleted is not set for that id in table2.

I have written function in my model:

public function getDistinctIds() {

    return DB::connection('pgsql')
                    ->table('table1 AS t1')
                    ->distinct()
                    ->select('t1.t2_id')
                    ->join('table2 AS t2', 't2.id', '=', 't1.t2_id')
                    ->where('t2.is_deleted', '!=', 1)
                    ->orWhereNull('t2.is_deleted')
                    ->paginate(3);
}

This function will give me accurate result. But there is problem with pagination. There are 12 records in table1 getDistinctIds() gives only 4 record so there should be two pages but it is showing 4 pages. I think pagination is not taking where condition into consideration.

Please help thanks in advance!!

I got the solution instead of distinct() used groupBy() . Now it is working fine. :)

Referenced from laracast post : [L4.2] distinct() and pagination returns total number of results not distinct total

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM