'laravel 8 - Spatie activity log v3 - call to undefined method App\Models\Report::user()

I am using spatie activity-log v3 to log activities done by users in the website.

In the User.php Model, I have the following code (which works correctly)

use App\Core\Traits\SpatieLogsActivity;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;
    use SpatieLogsActivity, LogsActivity;
    use HasRoles;

    /**
     * Log all activities performed by the user.
    */
    protected static $ignoreChangesAttributes = ['password'];
    protected static $logAttributes = ['name', 'email', 'phone', 'avatar'];
    protected static $spatieLogsActivity = ['name', 'email', 'phone', 'avatar'];
    protected static $recordEvents = ['created', 'updated', 'deleted'];
    protected static $logName = 'Users';

...

The code above, means I want spatie to track the 'name', 'email', 'phone', 'avatar' inputs when the user 'created', 'updated', 'deleted' them.

I am trying to implement the same functionality for another model: Report

namespace App\Models;

use App\Core\Traits\SpatieLogsActivity;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Report extends Model
{
    use HasFactory;
    use SpatieLogsActivity, LogsActivity;
    use HasRoles;

     /**
     * Log all activities performed by the user.
    */
    protected static $ignoreChangesAttributes = [''];
    protected static $spatieLogsActivity = ['caseType', 'caseLocation'];
    protected static $recordEvents = ['created', 'updated', 'deleted'];
    protected static $logName = 'Reports & Cases';

When I do some changes in the Report form and refresh the audit log, it shows me an error:

enter image description here

However!, when I delete previously inserted data and refresh, the log will actually show:

enter image description here



Solution 1:[1]

I just added the block of code in Report Model

/**
* Get a fullname combination of first_name and last_name
*
* @return string
*/
public function getNameAttribute()
{
 return "{$this->first_name} {$this->last_name}";
}

what I understood from the error is that the log cannot find the user aka causer of the events in the Report model.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdulrahman Mushref