owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
3.05k stars 390 forks source link

MassDestroy do not save log #613

Closed kinzinho666 closed 2 years ago

kinzinho666 commented 4 years ago

Laravel 7

Actual Behaviour

Do not save log when a use DataTables MassDestroy, if i remove just only one, log save OK.

Model friends.php

namespace App;

use App\Traits\Auditable;
use Yajra\Auditable\AuditableWithDeletesTrait;`

`class ProcedureType extends Model
{
    use SoftDeletes, MultiTenantModelTrait, Auditable, AuditableWithDeletesTrait;

   protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];
 protected $fillable = [
        'title',
        'created_at',
        'updated_at',
        'deleted_at',
        'created_by_id',
    ];

Controller app\http\admin\FriendsController.php

public function destroy(Friends $friends)
    {
        abort_if(Gate::denies('friends_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $friends->delete();

        return back();

    }
public function massDestroy(MassDestroyFriendsRequest $request)
    {
        Friends::whereIn('id', request('ids'))->delete();

        return response(null, Response::HTTP_NO_CONTENT);

    }

Steps to Reproduce

composer require yajra/laravel-auditable

composer update

open my friends page

select 2 or more on checkbox and click em Mass Destroy (datatables)

kinzinho666 commented 4 years ago

I try save manually, its possible?

$dados = [ProcedureType::whereIn('id', request('ids'))]; foreach ($dados as $d) { $d->delete(); AuditLog::deleted($d); //like this... how to do? }

MortenDHansen commented 2 years ago

The package works by observing the model events. When performing mass-updates the eloquent models are not being touched and the package cant know what goes on. You need to do a collection each or a for loop to achieve this