rappasoft / laravel-livewire-tables

A dynamic table component for Laravel Livewire
https://rappasoft.com/docs/laravel-livewire-tables/v2/introduction
MIT License
1.8k stars 340 forks source link

[Bug]: Duplicate value from different relationship but seeing the same table #1253

Closed andrersilva96 closed 1 year ago

andrersilva96 commented 1 year ago

What happened?

Duplicate value from different relationship but seeing the same table.

How to reproduce the bug

In a class/child table that has parent relationship, for example my class Matcher:

image

My datatable class has these columns: image

The result: image

As you can see in database: image

The properties table: image

I won't to show the users table because is the same id from profiles table ok... so this is the profiles table: image

The result should be: image

Package Version

2.14.0

PHP Version

8.1.x

Laravel Version

9

Alpine Version

3.10.5

Theme

Bootstrap 5.x

Notes

The result is the same for different relationship with same table, in the other words parent_id_from and parent_id_to for same table.

And Yes I've consulted your documentation: https://rappasoft.com/docs/laravel-livewire-tables/v2/columns/relationships

Error Message

None error.

lrljoe commented 1 year ago

Couple very quick question before I take a look in the next day or two -

Are the Database:

Are you using the Builder() approach or just a model name specified?

Have you added all of the relevant IDs into your setAdditionalSelects in the configure method, where you're not using them in a column?

andrersilva96 commented 1 year ago

Sorry take too long.

  1. bigint unsigned null
  2. utf8mb4
  3. utf8mb4_0900_ai_ci
  4. Yes
  5. Yes, as you can see bellow: image
  6. I didn't declare nothing: image

And this is the query generated:

SELECT 
    `matches`.`id` AS `id`,
    `properties`.`id` AS `propertyFrom.id`,
    `properties`.`id` AS `propertyTo.id`,
    `matches`.`created_at` AS `created_at`
FROM
    `matches`
        LEFT JOIN
    `properties` ON `matches`.`property_id_from` = `properties`.`id`
LIMIT 25 OFFSET 0
andrersilva96 commented 1 year ago

Solution:

To my column I called the label function and did something like:

->label(function ($row) use ($data) {
    $value = null;

    foreach (explode('.', $data['column']) as $attr) {
        $value = !$value ? $row->{$attr} : $value->{$attr};
    }

    return $value;
});

So in column I can call like "id" or "user.profile.name".

lrljoe commented 1 year ago

Glad you were able to fix it, but I'd definitely check your queries as you're possibly introducing multiple queries with your approach.

I'm a bit confused as to why you're checking if the model table is users in your Builder function, as you should surely know if it is or not when you create the Component...

You can use the following in your configure() method to add more fields that you're not directly referencing as columns, and then you'll be able to reference them as $row->fieldName, or whatever you choose.

$this->setAdditionalSelects(['someTable.someColumnName as fieldName']);
andrersilva96 commented 1 year ago

Thank you for helping, about the checking if the model table is users its because functionality of my system, instead of having several models of datatables I only have one and I pass the configuration with the model and the columns that I want in an array

lrljoe commented 1 year ago

Why not use a trait instead out of interest?

Otherwise if you're passing in things like columns as a variable, you run the risk of exposing something you don't want to to a malicious user.

andrersilva96 commented 1 year ago

It because I use the nwidart modules, so each client it's a different configuration array and the user doesn't has access.

lrljoe commented 1 year ago

As long as they're protected/private variables in your component then! If they're anything else, then the user will have access unless you're using another mechanism to protect the variable.

stale[bot] commented 1 year ago

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.

lrljoe commented 1 year ago

Just a quick note, I believe this is now fixed in v3.0, so I'd welcome your test when it comes to the beta in a week or two.

lrljoe commented 1 year ago

@andrersilva96 - The V3 Beta is out, if you're able to test it (it's Livewire 3.0+) then that'd be great, I can then look at backporting anything that isn't a breaking change into v2.