laravel-auditing icon indicating copy to clipboard operation
laravel-auditing copied to clipboard

What happens if I already have a table/model called Audits?

Open CristianSitov opened this issue 4 years ago • 1 comments

Q A
Bug? no
New Feature? yes
Framework Laravel
Framework version 7.x
Package version 10.x
PHP version 7.4.7

Actual Behaviour

Unable to implement Auditable on a model Company because it already has audits() relationship. Error: Declaration of App\\Models\\Company::audits() must be compatible with OwenIt\\Auditing\\Contracts\\Auditable::audits(): Illuminate\\Database\\Eloquent\\Relations\\MorphMany

Steps to Reproduce

Straight forward:

class Company extends Model implements Auditable
{
    use \OwenIt\Auditing\Auditable, SoftDeletes;

    public function audits()
    {
        return $this->hasMany(\App\Models\Audit::class, 'brand_id');
    }
}

Possible Solutions

Not a solution per se, but rather a condition: changing the existing audits() relationship would mean to affect hundreds of lines of code. I would rather like to have an option to change the Auditable::audits() (or to be changed) into something appropriate, more specific to this package.

CristianSitov avatar Jul 27 '20 23:07 CristianSitov

Might be a bit late answer, but i do agree that the package should claim a more specific table name to avoid potential collisions. It will be done in an upcoming revision

MortenDHansen avatar Feb 23 '22 14:02 MortenDHansen

What happens if I already have a table called Audits

that can be changed on config https://github.com/owen-it/laravel-auditing/blob/75b33ec9b4345e4539d94d6c28233b34c5d2730f/config/audit.php#L154

What happens if I already have a model called Audits?

Models have different namespaces, and can be aliased

unable to implement Auditable on a model Company because it already has audits() relationship.

Something like this is still pending, but I don't see the way to rename the relationship dynamically

parallels999 avatar Mar 09 '23 14:03 parallels999

Considered this for an upcoming release, but have decided not to make change to the table name. Table and model are themselves pretty easy to change. In one of my own applications i just changed the table and made a new model with a new name extending the Audit model.

With regards to the relation called 'audits', i don't currently have any good ideas to make it dynamic, or desire to obfuscate the name somehow.

MortenDHansen avatar Mar 15 '23 12:03 MortenDHansen