Laravel-auditing: Audit on a pivot model doesent work?

Created on 26 Nov 2017  路  3Comments  路  Source: owen-it/laravel-auditing

| Q | A
| ----------------- | ---
| Bug? | yes
| New Feature? | no
| Framework | Laravel
| Framework version | 5.4
| Package version | 4.1
| PHP version | 5.6

Actual Behaviour

Getting exception:

(1/1)聽ErrorExceptionArgument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model, none given, called in C:\xampp\htdocs\gdpr\src\gdpr\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasEvents.php on line 35 and defined
--
in聽Pivot.php聽(line 47)

Expected Behaviour

No exception

Steps to Reproduce

Add Auditable to a pivot model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Auditable;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;

class CarUser extends Pivot implements AuditableContract
{
    use SoftDeletes;
    use Auditable;

    public function role()
    {
        return $this->belongsTo('App\Role');
    }
}
help wanted invalid

Most helpful comment

I found a related issue upstream, with a classic GC reply + issue closed.

All 3 comments

Can you provide the full stack trace? The ErrorExceptionArgument message you've posted doesn't seem to be related with anything the package does.

Also, what happens if you remove the implements AuditableContract and use Auditable;, will it still blow up?

I just had a look, and this is an issue with Eloquent, not with this package. At least for now, it seems that Pivot models won't be able to be observed, due to the way the register process works.

The Pivot constructor, currently requires four arguments, with the last one as optional

public function __construct(Model $parent, $attributes, $table, $exists = false)

while the Model constructor, only has one argument with a default value, making it optional

public function __construct(array $attributes = [])

The issue happens in the observe() method of the HasEvents trait, where a new instance is created:

    public static function observe($class)
    {
        // This will work for Model, but not for Pivot, since 
        // the three required arguments are missing
        $instance = new static;
        // ...
    }

Pivot models might not be intended for observation in the first place, but you'll have to take it up a level and open an issue in the laravel/framework and see what upstream has to say.

I found a related issue upstream, with a classic GC reply + issue closed.

Was this page helpful?
0 / 5 - 0 ratings