kirschbaum-development / eloquent-power-joins

The Laravel magic you know, now applied to joins.
MIT License
1.39k stars 87 forks source link

laravel-splade: Can't sort relation column in SpladeTable #171

Closed p4rad0xus closed 7 months ago

p4rad0xus commented 7 months ago

I use Laravel Splade for a web project. To display the data from a database table I use the functionality of SpladeTable. The query is generated using joinRelationship().

The SpladeTable documentation explains how to sort by relationship column. When I want to use this and trigger the sorting, I get the following error:

To order the query using a column from a relationship, make sure the Model uses the 'PowerJoins' trait.

However, the use of the trait is stored in the models.

The query builder: Competition::joinRelationship('competitionType')

Configuration of the SpladeTable:

$table
    ->column(key: 'name', label: 'Title')
    ->column('competitionType.name', 'Typ', sortable: true)
    ->column('start_date', 'Start', sortable: true)
    ->column('end_date', 'End', sortable: true);

The models:

class CompetitionType extends Model
{
    use HasFactory;
    use HasUlids;
    use PowerJoins;

    public function competitions(): HasMany
    {
        return $this->hasMany(Competition::class);
    }
}
class Competition extends Model
{
    use HasFactory;
    use HasUlids;
    use PowerJoins;

    public function competitionType(): BelongsTo
    {
        return $this->belongsTo(CompetitionType::class);
    }
}

Do I have to use the trait in a different way in the model?

luisdalmolin commented 7 months ago

@p4rad0xus Did you try to include competitionType.name in the select in the query builder?

e.g.

Competition::query()
    ->select('competition.*')
    ->select('competitionType.name')
    ->joinRelationship('competitionType')...
luisdalmolin commented 7 months ago

Actually, nevermind this comment. Something seems off, but I believe it's in the Splade package itself. What version of power joins do you have installed, and what version of Splade?

luisdalmolin commented 7 months ago

Yeah, the package implementation is intended to work with power joins version 2.*. Version 3 doesn't work with the current implementation. Try to downgrade to 2 (you could hit version constraints), but then it should work. I'll try to work with the Splade team to add version 3 support.

luisdalmolin commented 7 months ago

I'm closing the issue for now, but feel free to re-open.