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

How can I audit delete options #136

Closed
cesarcruzc opened this issue Oct 24, 2016 · 11 comments
Closed

How can I audit delete options #136

cesarcruzc opened this issue Oct 24, 2016 · 11 comments

Comments

@cesarcruzc
Copy link

When I delete a record dont log in the table audits

@anteriovieira
Copy link
Member

Hi @cesarcruzc , I would like to better understand your case. Can you provide an example of what you are doing.

@cesarcruzc
Copy link
Author

Hi @anteriovieira.

I'm using Laravel 5.2 and working with repository pattern.

This is an example of my Models:

`
namespace App\Entities;

use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Auditable;
use Spatie\Activitylog\Traits\LogsActivity;

class AccidentCase extends Model
{

Use Auditable, LogsActivity;

protected $table = 'bdc_caso_accidente';

protected $fillable = ['nombres', 'telefono', 'celular', 'ciudad_id', 'bdc_datos_trabajador_id', 'entry_by'];

protected $guarded = ['id'];

// LogsActivity
protected static $logAttributes = ['nombres', 'telefono', 'celular', 'ciudad_id', 'bdc_datos_trabajador_id', 'entry_by'];


public function getLogNameToUse(string $eventName = ''): string
{
    return $this->table;
}
//

// Auditable
protected $auditableTypes = ['created', 'saved', 'deleted'];
//


// Mutators

public function getNombresAttribute($value)
{
    return title_case(strtolower($value));
}

//--

// Relations

public function workerData()
{
    return $this->belongsTo(WorkerData::class, 'bdc_datos_trabajador_id');
}

// --

}
`

@cesarcruzc
Copy link
Author

I am testing this package https://github.com/spatie/laravel-activitylog and I have the same issue.

am I doing something wrong?

@anteriovieira
Copy link
Member

As you are deleting the model?

Because have some queries that laravel does not run events.

@anteriovieira
Copy link
Member

This should work.

$AccidentCase = AccidentCase::find(1);

$AccidentCase ->delete();

@cesarcruzc
Copy link
Author

cesarcruzc commented Oct 28, 2016

My Repository:

class AccidentCaseRepository extends BaseRepository
{

public function getModel() { return new AccidentCase(); }

public function deleteDataByWorker($workerId) { $model = $this->getModel(); return $model->where('bdc_datos_trabajador_id', $workerId)->delete(); }

@anteriovieira
Copy link
Member

This is the same case reported in issues 139.

@anteriovieira
Copy link
Member

The solution is the following:

class AccidentCaseRepository extends BaseRepository 
{ 
    protected $accidentCase;

    public function __construct(AccidentCase $accidentCase)
    {
        $this->accidentCase = $accidentCase;
    }

    public function deleteDataByWorker($workerId) 
    { 
        $accidentCase = $this->accidentCase->find($workerId);

        return $accidentCase->delete();
    } 
}

@anteriovieira
Copy link
Member

ops!

@anteriovieira
Copy link
Member

anteriovieira commented Oct 28, 2016

class AccidentCaseRepository extends BaseRepository 
{ 
    protected $accidentCase;

    public function __construct(AccidentCase $accidentCase)
    {
        $this->accidentCase = $accidentCase;
    }

    public function deleteDataByWorker($workerId) 
    { 
        $accidentCase = $this->accidentCase
                             ->where('bdc_datos_trabajador_id', $workerId)
                             ->firstOrFail();

        return $accidentCase->delete();
    } 
}

@cesarcruzc
Copy link
Author

@anteriovieira Thank you so much!
This solution worked for me!!

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

2 participants