JABirchall's avatar

Eloquent sqlsrv Join Pagination error

I'm getting this error when doing pagination on a joined query specifically when requesting any page but 1.

My query:

$accounts = Accounts::join('UsersData', 'Accounts.CustomerID', '=', 'UsersData.CustomerID')
 ->simplePaginate(20);
 return view('accounts', compact('accounts'));

and I'm getting error:

QueryException in Connection.php line 624:
SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]The column 'CustomerID' was specified multiple times for 'temp_table'.
(SQL: select * from (select *, row_number() over (order by (select 0)) as row_num from [accounts] inner join [UsersData] on [Accounts].[CustomerID] = [UsersData].[CustomerID]) as temp_table where row_num between 21 and 41)

Anyone able to help me, thanks.

0 likes
1 reply
TrederusMaximus's avatar

This should do the trick:

$accounts = Accounts::select('accounts.*')
->join('UsersData', 'Accounts.CustomerID', '=', 'UsersData.CustomerID')
 ->simplePaginate(20);
 return view('accounts', compact('accounts'));

2 likes

Please or to participate in this conversation.