Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Russelmhardy's avatar

Eloquent query for retrieving data from Partitioned Table

How to Write Eloquent query for retrieving data from Partitioned Table

I am working on database partitioning and using laravel framework for development. I am trying to convert raw query to Eloquent.

Ex. My query is like SELECT * FROM employee PARTITION (p2) so using raw queries its like

$results = DB::select( DB::raw("SELECT * FROM employee PARTITION (p2)") );

I am not able to convert it if I use Eloquent as in Eloquent query syntax is like ClassName::select('*')->where()->get(); and my query does not contain where clause.

What is the way to convert this query? Any help would be deeply appreciated.

0 likes
4 replies
Sinnbeck's avatar

To convert the query to an Eloquent statement in Laravel, you can use the whereRaw method to specify the partition in the query. For example, the equivalent Eloquent statement would be:

$results = Employee::select('*')->whereRaw('PARTITION (p2)')->get();
1 like
Russelmhardy's avatar

@Sinnbeck Thanks for Repley and Sorry for my late comment this code doesn't work: it's gives me this error :

SQLSTATE[42703]: Undefined column: 7 ERROR: column \"PARTITION (p2)\" does not exist\nLINE 1

Please or to participate in this conversation.