laravel-livewire-tables icon indicating copy to clipboard operation
laravel-livewire-tables copied to clipboard

[2.0] Relationship error

Open jantbox opened this issue 2 years ago • 18 comments

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.

jantbox avatar Apr 02 '22 16:04 jantbox

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 joinusersonusers.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

pablomadariaga avatar Apr 12 '22 17:04 pablomadariaga

In V1 there is a merged PR of mine that solves this issue allowing full qualified table names

fabio-ivona avatar Apr 12 '22 17:04 fabio-ivona

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

pablomadariaga avatar Apr 12 '22 17:04 pablomadariaga

Yep, I was suggesting to look at here for a new PR addressing the issue

fabio-ivona avatar Apr 12 '22 17:04 fabio-ivona

Thank you

pablomadariaga avatar Apr 12 '22 17:04 pablomadariaga

Column::make('company', 'company.name')
                ->eagerLoadRelations()

manusiakemos avatar May 01 '22 09:05 manusiakemos

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.

Screen Shot 2022-05-01 at 14 01 15

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

rappasoft avatar May 01 '22 17:05 rappasoft

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.

pablomadariaga avatar May 02 '22 13:05 pablomadariaga

Ok I understand and I can recreate, I just have to figure out a fix.

rappasoft avatar May 03 '22 01:05 rappasoft

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.

stale[bot] avatar Jun 04 '22 19:06 stale[bot]

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),

emilsrits avatar Jun 08 '22 09:06 emilsrits

We have the same issue. Any fix available?

PieterCappelle avatar Jun 08 '22 19:06 PieterCappelle

I have the same issue any work around on this issue

gozbert avatar Jul 05 '22 19:07 gozbert

Same issue

DanielGSoftware avatar Jul 14 '22 15:07 DanielGSoftware

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

pablomadariaga avatar Aug 08 '22 01:08 pablomadariaga

Also having this issue with a self join... Would love a workaround or a fix

bLLRR avatar Aug 08 '22 12:08 bLLRR

I'm having this issue as well.

My workaround is to use the query builder to construct the joins manually and select from there.

notscottsmith avatar Aug 27 '22 07:08 notscottsmith

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

julianR92 avatar Sep 09 '22 22:09 julianR92

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???

puntogestioncl avatar Sep 20 '22 18:09 puntogestioncl

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

esouflakis avatar Oct 11 '22 14:10 esouflakis

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.

stale[bot] avatar Nov 10 '22 23:11 stale[bot]