lavina's avatar

backpack crud ordering on a pivot table

Hello guys,

i am using backpack and attempting to display a crud admin panel for a pivot table(does not have its own id). But i get the error:

message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'order clause' (SQL: select * from `section_user` order by 'id` desc limit 10)"

I attempted to add in setup:

$this->crud->orderBy('user_id');

but now its attempting to sort twice:

message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'order clause' (SQL: select * from `section_user` order by `user_id` asc, `id` desc limit 10)"

How can i override and remove the sort by id?

0 likes
2 replies
Moham's avatar
Moham
Best Answer
Level 4

I think that happening because eloquent assume that each table has a primary key column named id

So you can override it useing $primaryKey variable in the model.

// here you'll override The column from id to user_id
protected $primaryKey = 'user_id';

For more you can see eloquent model conventions https://laravel.com/docs/master/eloquent#eloquent-model-conventions

1 like

Please or to participate in this conversation.