laravel-livewire-tables: [2.0] Relationship error

There is a problem when you have two relationships with the same table it always shows the first relationship. I’ll try to explain:

I have a model like this:

class Employee extends Model
{
    ...
    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class, 'company_id', 'id');
    }

    public function related_company(): BelongsTo
    {
        return $this->belongsTo(Company::class, 'related_company_id', 'id');
    }
    ...
}

I have a UserTable component with this piece of code

...
Column::make("Id", "id"),
Column::make("Com. Id.", "company_id"),
Column::make("Company", "company.name"),
Column::make("Rel. Id.", "related_company_id"),
Column::make("Rel. Company", "related_company.name"),
...

It shows something like this… livewire_tables_problem_1

The last column in the first line shows “Company 3” when related_company_id = 2. If I remove two first lines, I have something like this

livewire_tables_problem_2

In this case, the column shows the correct value of the related table.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 21 (6 by maintainers)

Most upvoted comments

Have you tried as workaround:

Column::make("Company")->label(fn ($row) => optional($row->company)->name),
Column::make("Rel. Company")->label(fn ($row) => optional($row->related_company)->name),

Hello, I have exactly the same problem. I need to show 3 users who participate in the approval of some documents, by stages, but it only shows the first one in the 3 columns. Has anyone been able to solve the problem???

Hello, how can I call a data from my related table, it does not work as the documentation says

 public function builder(): Builder
    {   $usuario = User::findOrFail(auth()->user()->id);
        if($usuario->getRoleNames()[0] == 'ADMIN'){
            return User::query()->where('role_id','!=' ,1)->where('role_id','!=',2)->leftjoin('roles', 'roles.id', 'users.role_id');      
         } else {
            return User::query()->leftjoin('roles', 'roles.id', 'users.role_id');
         }
    }

my function :

public function columns(): array
    {
        return [
            Column::make("Id", "id")
                ->sortable(),
                Column::make('Nombre','first_name')
                ->sortable(),
                Column::make('Apellidos','last_name')
                ->sortable(),
                Column::make('Usuario','email')
                ->sortable(),
                Column::make('Rol','roles.name')
                ->sortable(),
                            
            
        ];
    }

the error

Rappasoft\LaravelLivewireTables\Views\Column::setTable(): Argument #1 ($table) must be of type string, null given, called in C:\laragon\www\appema\vendor\rappasoft\laravel-livewire-tables\src\Traits\Helpers\ColumnHelpers.php on line 24

help me please

Thank you