Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Cannot use table with Spatie Laravel-activitylog #1931

Open
isigillo opened this issue Sep 6, 2024 · 6 comments
Open

[Bug]: Cannot use table with Spatie Laravel-activitylog #1931

isigillo opened this issue Sep 6, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@isigillo
Copy link

isigillo commented Sep 6, 2024

What happened?

The table component does not work with Activity model by spatie nor with a builder query

How to reproduce the bug

  1. Install https://spatie.be/docs/laravel-activitylog (v. 4.8)
  2. try to show log data with laravel-livewire-tables

Package Version

3.4.17

PHP Version

8.1.x

Laravel Version

10.48

Alpine Version

No response

Theme

None

Notes

<?php

namespace App\Livewire;


use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\DateColumn;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Traits\LogsActivity;

class LogTable extends DataTableComponent
{
    protected $model = Activity::class;
    /*public function builder(): Builder {
        return Activity::query()
            ->join('users', 'users.id', '=', 'activity_log.causer_id', 'left')
            ->select(['activity_log.id','description', 'subject_type', 'subject_id', 'event', 'users.name', 'properties', 'created_at']); // Select some things
    }*/

    public function configure(): void
    {
        $this->setPrimaryKey('id');
        $this->setDefaultSort('id', 'desc');

        $this->setPerPageAccepted([25, 50, 100]);
        $this->setPerPage(25);

        $this->setDebugStatus(config('goldportal.debug'));
    }




    public function columns(): array
    {
        return [
            Column::make("ID", "id")
                ->sortable(),

            Column::make(__('tipo entità'), "subject_type")
                ->searchable()
                ->sortable(),

            Column::make(__('ID entità'), "subject_id")
                ->searchable()
                ->sortable(),

            DateColumn::make(ucfirst(__('data')), "activity_log.created_at")
                ->outputFormat('d-m-Y H:i:s')
                ->sortable(),




        ];
    }



}

Error Message

Call to undefined relationship [activity_log] on model [Spatie\Activitylog\Models\Activity].
https://flareapp.io/share/Lm8lKa1m

@isigillo isigillo added the bug Something isn't working label Sep 6, 2024
@lrljoe
Copy link
Sponsor Collaborator

lrljoe commented Sep 9, 2024

            DateColumn::make(ucfirst(__('data')), "activity_log.created_at")
                ->outputFormat('d-m-Y H:i:s')
                ->sortable(),

Should be

            DateColumn::make(ucfirst(__('data')), "created_at")
                ->outputFormat('d-m-Y H:i:s')
                ->sortable(),

Otherwise it looks for an "activity_log" relation on the Activity model, which doesn't exist.

@isigillo
Copy link
Author

Thank you.

It works, but I have another issue: how can I show user name?
activity_log table has a causer_id and Spatie Activity model has ha "causer" relation but it does not work
I get this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'causer.' in 'on clause'

select
  count(*) as aggregate
from
  `activity_log`
  left join `activity_log` as `causer` on `activity_log`.`causer_id` = `causer`.``

@lrljoe
Copy link
Sponsor Collaborator

lrljoe commented Sep 12, 2024

Share what you have setup in configure and the column you're trying to use for the user name

If you're using builder() approach then also please share that

@isigillo
Copy link
Author

I tried with both the model approach and the builder

public function builder(): Builder {
        return Activity::query()
            ->with(['causer'])
            ->select(); 
    }

This is configure

public function configure(): void
    {
        $this->setPrimaryKey('id');
        $this->setDefaultSort('id', 'desc');

        $this->setPerPageAccepted([25, 50, 100]);
        $this->setPerPage(25);

        $this->setTdAttributes(function (Column $column) {
            if ($column->isField('properties')) {
                return [
                    'default' => false,
                    'class' => 'w-80 text-xs',
                ];
            }

            return [];
        });

        $this->setDebugStatus(config('goldportal.debug'));
    }

This is the column

Column::make('user', "causer.name"),

@lrljoe
Copy link
Sponsor Collaborator

lrljoe commented Sep 14, 2024

If you use a label, then you'll be able to do it

Keep in mind that ActivityLog being a morphTo, you'll have an n+1 query. But it'll only be the two queries running

@isigillo
Copy link
Author

If you use a label

I am not sure to understand. Do you mean something like this?

Column::make('user',"causer_id")
                ->label(fn($row) => User::find($row->causer_id)->name),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants