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

I get an error when trying to update my user model when using the RevisionableTrait #436

Open
lnieuwenhuis opened this issue Dec 5, 2024 · 1 comment

Comments

@lnieuwenhuis
Copy link

Relevant part of the user model:

<?php

namespace App\Models;

use App\Enum\RoleEnum;
use App\Observers\UserObserver;
use App\Scopes\UserScope;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Venturecraft\Revisionable\RevisionableTrait;

#[ObservedBy([UserObserver::class])]
class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasRoles;
    use Notifiable;
    use SoftDeletes;
    use RevisionableTrait;

Error I get from RevisionableTrait when trying to update anything on any user:

{
  "errors": [
    {
      "message": "Internal server error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "upsertUser"
      ],
      "extensions": {
        "debugMessage": "Call to undefined method Event::dispatch()",
        "file": "/var/www/html/vendor/venturecraft/revisionable/src/Venturecraft/Revisionable/RevisionableTrait.php",
        "line": 214,
        "trace": [
          {
            "file": "/var/www/html/vendor/venturecraft/revisionable/src/Venturecraft/Revisionable/RevisionableTrait.php",
            "line": 77,
            "call": "App\\Models\\User::postSave()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php",
            "line": 458,
            "call": "App\\Models\\User::Venturecraft\\Revisionable\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php",
            "line": 286,
            "call": "Illuminate\\Events\\Dispatcher::Illuminate\\Events\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php",
            "line": 266,
            "call": "Illuminate\\Events\\Dispatcher::invokeListeners()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php",
            "line": 215,
            "call": "Illuminate\\Events\\Dispatcher::dispatch()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
            "line": 1181,
            "call": "Illuminate\\Database\\Eloquent\\Model::fireModelEvent()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
            "line": 1154,
            "call": "Illuminate\\Database\\Eloquent\\Model::finishSave()"
          },
          {
            "file": "/var/www/html/app/GraphQL/Resolvers/UserResolver.php",
            "line": 39,
            "call": "Illuminate\\Database\\Eloquent\\Model::save()"
          },
          {
            "file": "/var/www/html/app/GraphQL/Directives/UseGuardedDirective.php",
            "line": 47,
            "call": "App\\GraphQL\\Resolvers\\UserResolver::upsertUser()"
          },
          {
            "file": "/var/www/html/app/GraphQL/Directives/HasRoleDirective.php",
            "line": 43,
            "call": "App\\GraphQL\\Directives\\UseGuardedDirective::App\\GraphQL\\Directives\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/Auth/GuardDirective.php",
            "line": 56,
            "call": "App\\GraphQL\\Directives\\HasRoleDirective::App\\GraphQL\\Directives\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/Schema/Values/FieldValue.php",
            "line": 185,
            "call": "Nuwave\\Lighthouse\\Auth\\GuardDirective::Nuwave\\Lighthouse\\Auth\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
            "line": 714,
            "call": "Nuwave\\Lighthouse\\Schema\\Values\\FieldValue::Nuwave\\Lighthouse\\Schema\\Values\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
            "line": 631,
            "call": "GraphQL\\Executor\\ReferenceExecutor::resolveFieldValueOrError()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
            "line": 541,
            "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
            "line": 949,
            "call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}()"
          },
          {
            "call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
            "line": 941,
            "function": "array_reduce()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
            "line": 533,
            "call": "GraphQL\\Executor\\ReferenceExecutor::promiseReduce()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
            "line": 297,
            "call": "GraphQL\\Executor\\ReferenceExecutor::executeFieldsSerially()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
            "line": 237,
            "call": "GraphQL\\Executor\\ReferenceExecutor::executeOperation()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/Executor.php",
            "line": 159,
            "call": "GraphQL\\Executor\\ReferenceExecutor::doExecute()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/GraphQL.php",
            "line": 162,
            "call": "GraphQL\\Executor\\Executor::promiseToExecute()"
          },
          {
            "file": "/var/www/html/vendor/webonyx/graphql-php/src/GraphQL.php",
            "line": 96,
            "call": "GraphQL\\GraphQL::promiseToExecute()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/GraphQL.php",
            "line": 114,
            "call": "GraphQL\\GraphQL::executeQuery()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/GraphQL.php",
            "line": 86,
            "call": "Nuwave\\Lighthouse\\GraphQL::executeParsedQuery()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/GraphQL.php",
            "line": 208,
            "call": "Nuwave\\Lighthouse\\GraphQL::executeQueryString()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/GraphQL.php",
            "line": 173,
            "call": "Nuwave\\Lighthouse\\GraphQL::executeOperation()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/Support/Utils.php",
            "line": 107,
            "call": "Nuwave\\Lighthouse\\GraphQL::Nuwave\\Lighthouse\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/GraphQL.php",
            "line": 169,
            "call": "Nuwave\\Lighthouse\\Support\\Utils::mapEach()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/Http/GraphQLController.php",
            "line": 32,
            "call": "Nuwave\\Lighthouse\\GraphQL::executeOperationOrOperations()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
            "line": 46,
            "call": "Nuwave\\Lighthouse\\Http\\GraphQLController::__invoke()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line": 260,
            "call": "Illuminate\\Routing\\ControllerDispatcher::dispatch()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line": 206,
            "call": "Illuminate\\Routing\\Route::runController()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 806,
            "call": "Illuminate\\Routing\\Route::run()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 144,
            "call": "Illuminate\\Routing\\Router::Illuminate\\Routing\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/Http/Middleware/AttemptAuthentication.php",
            "line": 22,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 183,
            "call": "Nuwave\\Lighthouse\\Http\\Middleware\\AttemptAuthentication::handle()"
          },
          {
            "file": "/var/www/html/vendor/nuwave/lighthouse/src/Http/Middleware/AcceptJson.php",
            "line": 24,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 183,
            "call": "Nuwave\\Lighthouse\\Http\\Middleware\\AcceptJson::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 119,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 805,
            "call": "Illuminate\\Pipeline\\Pipeline::then()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 784,
            "call": "Illuminate\\Routing\\Router::runRouteWithinStack()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 748,
            "call": "Illuminate\\Routing\\Router::runRoute()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 737,
            "call": "Illuminate\\Routing\\Router::dispatchToRoute()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 200,
            "call": "Illuminate\\Routing\\Router::dispatch()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 144,
            "call": "Illuminate\\Foundation\\Http\\Kernel::Illuminate\\Foundation\\Http\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 21,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php",
            "line": 31,
            "call": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 183,
            "call": "Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 21,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php",
            "line": 50,
            "call": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 183,
            "call": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Http/Middleware/ValidatePostSize.php",
            "line": 27,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 183,
            "call": "Illuminate\\Http\\Middleware\\ValidatePostSize::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php",
            "line": 110,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 183,
            "call": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php",
            "line": 62,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 183,
            "call": "Illuminate\\Http\\Middleware\\HandleCors::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php",
            "line": 57,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 183,
            "call": "Illuminate\\Http\\Middleware\\TrustProxies::handle()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 119,
            "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 175,
            "call": "Illuminate\\Pipeline\\Pipeline::then()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 144,
            "call": "Illuminate\\Foundation\\Http\\Kernel::sendRequestThroughRouter()"
          },
          {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
            "line": 1168,
            "call": "Illuminate\\Foundation\\Http\\Kernel::handle()"
          },
          {
            "file": "/var/www/html/public/index.php",
            "line": 17,
            "call": "Illuminate\\Foundation\\Application::handleRequest()"
          }
        ]
      }
    }
  ],
  "data": {
    "upsertUser": null
  }
}

Somehow the event function is broken. I haven't dug too deep into the code, but I don't really have any time to do so.

@lnieuwenhuis
Copy link
Author

As a side-note, here is the part of the RevisionableTrait that is causing the error:

public function postSave()
    {
        if (isset($this->historyLimit) && $this->revisionHistory()->count() >= $this->historyLimit) {
            $LimitReached = true;
        } else {
            $LimitReached = false;
        }
        if (isset($this->revisionCleanup)){
            $RevisionCleanup=$this->revisionCleanup;
        }else{
            $RevisionCleanup=false;
        }

        // check if the model already exists
        if (((!isset($this->revisionEnabled) || $this->revisionEnabled) && $this->updating) && (!$LimitReached || $RevisionCleanup)) {
            // if it does, it means we're updating

            $changes_to_record = $this->changedRevisionableFields();

            $revisions = array();

            foreach ($changes_to_record as $key => $change) {
                $original = array(
                    'revisionable_type' => $this->getMorphClass(),
                    'revisionable_id' => $this->getKey(),
                    'key' => $key,
                    'old_value' => Arr::get($this->originalData, $key),
                    'new_value' => $this->updatedData[$key],
                    'user_id' => $this->getSystemUserId(),
                    'created_at' => new \DateTime(),
                    'updated_at' => new \DateTime(),
                );

                $revisions[] = array_merge($original, $this->getAdditionalFields());
            }

            if (count($revisions) > 0) {
                if($LimitReached && $RevisionCleanup){
                    $toDelete = $this->revisionHistory()->orderBy('id','asc')->limit(count($revisions))->get();
                    foreach($toDelete as $delete){
                        $delete->delete();
                    }
                }
                $revision = Revisionable::newModel();
                \DB::table($revision->getTable())->insert($revisions);
                \Event::dispatch('revisionable.saved', array('model' => $this, 'revisions' => $revisions));
            }
        }
    }

All the way at the bottom, the

                \Event::dispatch('revisionable.saved', array('model' => $this, 'revisions' => $revisions));

Is where it goes wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant