Test Test
Test Test

Reputation: 79

How to override a trait function in another trait?

I am using laravel framework to develop API's, i am using laravel-saptie audit package to monitor all the users activity.now i have to modify or add the some functionality in LogsActivity Trait boot method ,after some research i am using like following

LogsActivity.php

trait LogsActivity{
protected static function bootLogsActivity(): void
    {
        // Hook into eloquent events that only specified in $eventToBeRecorded array,
        // checking for "updated" event hook explicitly to temporary hold original
        // attributes on the model as we'll need them later to compare against.

        static::eventsToBeRecorded()->each(function ($eventName) {
            if ($eventName === 'updated') {
                static::updating(function (Model $model) {
                    $oldValues = (new static())->setRawAttributes($model->getRawOriginal());
                    $model->oldAttributes = static::logChanges($oldValues);
                });
            }

            static::$eventName(function (Model $model) use ($eventName) {
                $model->activitylogOptions = $model->getActivitylogOptions();

                if (! $model->shouldLogEvent($eventName)) {
                    return;
                }

                $changes = $model->attributeValuesToBeLogged($eventName);

                $description = $model->getDescriptionForEvent($eventName);

                $logName = $model->getLogNameToUse();

                // Submitting empty description will cause place holder replacer to fail.
                if ($description == '') {
                    return;
                }

                if ($model->isLogEmpty($changes) && ! $model->activitylogOptions->submitEmptyLogs) {
                    return;
                }

                // User can define a custom pipelines to mutate, add or remove from changes
                // each pipe receives the event carrier bag with changes and the model in
                // question every pipe should manipulate new and old attributes.
                $event = app(Pipeline::class)
                    ->send(new EventLogBag($eventName, $model, $changes, $model->activitylogOptions))
                    ->through(static::$changesPipes)
                    ->thenReturn();

                // Actual logging
                $logger = app(ActivityLogger::class)
                    ->useLog($logName)
                    ->event($eventName)
                    ->performedOn($model)
                    ->withProperties($event->changes);

                if (method_exists($model, 'tapActivity')) {
                    $logger->tap([$model, 'tapActivity'], $eventName);
                }

                $logger->log($description);

                // Reset log options so the model can be serialized.
                $model->activitylogOptions = null;
            });
        });
    }
}
<?php

namespace App\Http\Traits;

use ReflectionMethod;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Traits\LogsActivity;

trait CustomLogTrait{
    use LogsActivity
    {
        LogsActivity::bootLogsActivity as parentbootLogsActivity;
    }

    protected static $logOnlyDirty = true;

    public static function bootLogsActivity(){
$this->parentbootLogsActivity;
        static::creating(function(Activity $model){
            $act = $model->all()->last();
            $act;
        });
    }
}

i am facing this problem Using $this when not in object context {"exception":"[object] (Error(code: 0): Using $this when not in object context.instead of resolving this one if i use directly in customTrait inside bootLogsActivity() function like this

LogsActivity::bootLogsActivity

still this one also throwing an error like protected one can't able to access.can anyone help to me override the LogsActivity boot method inside my customLogTrait ?

Thanks in Advance !

Upvotes: 0

Views: 289

Answers (1)

Sachin Bahukhandi
Sachin Bahukhandi

Reputation: 2536

You are trying to access this from a static context.

Thus, the line:

$this->parentbootLogsActivity;      

Shall be modified to:

self::parentbootLogsActivity;
       

Upvotes: 1

Related Questions