laravel-livewire-tables
laravel-livewire-tables copied to clipboard
[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...
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

In this case, the column shows the correct value of the related table.
Rappa I think that to solve this problem, you should allow aliases to be configured for each table that is presented within the query, be it the main table or the relationship tables.
I have a relationship of a column in the same users table to know who invited them to join my platform, and this generates the error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'users'
When trying to perform this query:
select count(*) as aggregate from
usersleft join
userson
users.
referrer_id=
users.
id``
That is why I think that if the aliases of each table could be configured, the problem would be solved, if there is already a solution at the code level, I would like to receive a bit of your experience.
You did an excellent job, thank you for this wonderful tool. @rappasoft
In V1 there is a merged PR of mine that solves this issue allowing full qualified table names
I just forgot to mention that, I just updated to v2 and ran into this problem, but the idea is to always have the updated version @fabio-ivona
Yep, I was suggesting to look at here for a new PR addressing the issue
Thank you
Column::make('company', 'company.name')
->eagerLoadRelations()
Hmm, I do not have this issue? I made a table with 2 relationships with a name column, and a name column on the base table, and they all show up correctly.

Here's the resulting query:
select `users`.`id` as `id`, `users`.`name` as `name`, `addresses`.`address` as `address.address`, `addresses`.`name` as `address.name`, `address_groups`.`name` as `address.group.name`, `users`.`id` as `id`, `users`.`name` as `name`, `addresses`.`address` as `address.address`, `addresses`.`name` as `address.name`, `address_groups`.`name` as `address.group.name` from `users` left join `addresses` on `addresses`.`user_id` = `users`.`id` left join `address_groups` on `addresses`.`address_group_id` = `address_groups`.`id` limit 10 offset 0
The problem happens when using a relationship on the same table, for example:
users INNER JOIN users
With other relations like in your previous case "Address" there is no problem, in my case I need to show the name of the user who recommended my site to the new user.
Ok I understand and I can recreate, I just have to figure out a fix.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
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),
We have the same issue. Any fix available?
I have the same issue any work around on this issue
Same issue
Hello @rappasoft , is there a solution for this? I wrote months ago and I still haven't abandoned your package because I believe in all the potential it has, thank you very much
Also having this issue with a self join... Would love a workaround or a fix
I'm having this issue as well.
My workaround is to use the query builder to construct the joins manually and select from there.
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
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. I had the exact same problem. I have a table that has: 2 foreign keys that pinpoint to another(same) table: "translations" and 2 foreign keys that have relations ->categories->translation & ->specialCategories->translation that eventually those 2 foreign keys pinpoint to the same table: "translations".
The one and only workaround that seems to be working perfectly well is to ->format(function($value, $column, $row){}) each column. Create a "one div" blade.php view file and call it from the format cells with the field value like this: return view('layouts.table-field')->with('tableField', $valueField); That works for me. Sorry for the bad english! :P
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.