Skip to content

BelongsToMany relationship with orderBy returns duplicate column name created_at #2705

Closed
@dragonfly4

Description

@dragonfly4

Summary of problem or feature request

Displaying list of records from Pivot Table works. Basic example with a belongsToMany in the model.

$model->abilities();
    public function abilities()
    {
        return $this->belongsToMany(Ability::class);
    }

Add orderBy results in duplicate column error. The Pivot Table has timestamp columns.

$model->abilities()->orderBy('ability.name');

error: "Exception Message:\n\nSQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created_at' (SQL: select count(*) as aggregate from (select * from abilities inner join ability_role on abilities.id = ability_role.ability_id where ability_role.role_id = 2 order by abilities.name asc) count_row_table)"

The same command executes properly in Tinkerwell / Laravel Controller. But not via laravel-datatables. Maybe related to #1739 ?

Quick fix for me was to add a select to exclude the created_at + updated_at columns.

$model->abilities()->select('id','name','label')->orderBy('ability.name');

System details

  • Operating System: WAMPSERVER
  • PHP Version: 8.0.11
  • Laravel Version: 8.68.1
  • Laravel-Datatables Version: 9.18.2

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions