fix:edit logs error
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\ActivityLog;
|
||||||
|
use App\Models\Flight;
|
||||||
|
use App\Models\Tour;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class ActivityLogController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$logs = ActivityLog::query()
|
||||||
|
->whereIn('model_type', [Tour::class, Flight::class])
|
||||||
|
->with('admin')
|
||||||
|
->when($request->filled('model_type'), function ($query) use ($request) {
|
||||||
|
|
||||||
|
$query->where('model_type', $request->input('model_type'));
|
||||||
|
|
||||||
|
})
|
||||||
|
->when($request->filled('action'), function ($query) use ($request) {
|
||||||
|
|
||||||
|
$query->where('action', $request->input('action'));
|
||||||
|
|
||||||
|
})
|
||||||
|
->orderByDesc('created_at')
|
||||||
|
->paginate(20)
|
||||||
|
->withQueryString()
|
||||||
|
->through(function ($log) {
|
||||||
|
|
||||||
|
$modelClass = $log->model_type;
|
||||||
|
|
||||||
|
$relatedModel = $modelClass::find($log->model_id);
|
||||||
|
|
||||||
|
$log->model_title = $relatedModel->title ?? '(حذف شده)';
|
||||||
|
|
||||||
|
$log->model_label = $log->model_type === Tour::class
|
||||||
|
? 'تور'
|
||||||
|
: 'پرواز';
|
||||||
|
|
||||||
|
$log->model_edit_route = null;
|
||||||
|
|
||||||
|
if ($relatedModel) {
|
||||||
|
$log->model_edit_route = $log->model_type === Tour::class
|
||||||
|
? route('admin.tours.edit', $log->model_id)
|
||||||
|
: route('admin.flights.edit', $log->model_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $log;
|
||||||
|
});
|
||||||
|
|
||||||
|
return view('admin.activity-logs.index', compact('logs'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class ActivityLog extends Model
|
||||||
|
{
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'admin_id',
|
||||||
|
'model_type',
|
||||||
|
'model_id',
|
||||||
|
'action',
|
||||||
|
'changes',
|
||||||
|
'created_at',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'changes' => 'array',
|
||||||
|
'created_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* رابطه: هر لاگ متعلق به یک ادمین است.
|
||||||
|
*/
|
||||||
|
public function admin()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Admin::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Reliese Model.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class AdminPermission
|
||||||
|
*
|
||||||
|
* @property int $id
|
||||||
|
* @property int $admin_id
|
||||||
|
* @property bool $admin_view
|
||||||
|
* @property bool $admin_create
|
||||||
|
* @property bool $admin_edit
|
||||||
|
* @property bool $admin_delete
|
||||||
|
* @property Carbon|null $created_at
|
||||||
|
* @property Carbon|null $updated_at
|
||||||
|
* @property bool $user_view
|
||||||
|
* @property bool $user_edit
|
||||||
|
* @property bool $user_delete
|
||||||
|
* @property bool $tour_view
|
||||||
|
* @property bool $tour_create
|
||||||
|
* @property bool $tour_edit
|
||||||
|
* @property bool $tour_delete
|
||||||
|
* @property bool $flight_view
|
||||||
|
* @property bool $flight_create
|
||||||
|
* @property bool $flight_edit
|
||||||
|
* @property bool $flight_delete
|
||||||
|
*
|
||||||
|
* @package App\Models
|
||||||
|
*/
|
||||||
|
class AdminPermission extends Model
|
||||||
|
{
|
||||||
|
protected $table = 'admin_permission';
|
||||||
|
public static $snakeAttributes = false;
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'admin_id' => 'int',
|
||||||
|
'admin_view' => 'bool',
|
||||||
|
'admin_create' => 'bool',
|
||||||
|
'admin_edit' => 'bool',
|
||||||
|
'admin_delete' => 'bool',
|
||||||
|
'user_view' => 'bool',
|
||||||
|
'user_edit' => 'bool',
|
||||||
|
'user_delete' => 'bool',
|
||||||
|
'tour_view' => 'bool',
|
||||||
|
'tour_create' => 'bool',
|
||||||
|
'tour_edit' => 'bool',
|
||||||
|
'tour_delete' => 'bool',
|
||||||
|
'flight_view' => 'bool',
|
||||||
|
'flight_create' => 'bool',
|
||||||
|
'flight_edit' => 'bool',
|
||||||
|
'flight_delete' => 'bool'
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'admin_id',
|
||||||
|
'admin_view',
|
||||||
|
'admin_create',
|
||||||
|
'admin_edit',
|
||||||
|
'admin_delete',
|
||||||
|
'user_view',
|
||||||
|
'user_edit',
|
||||||
|
'user_delete',
|
||||||
|
'tour_view',
|
||||||
|
'tour_create',
|
||||||
|
'tour_edit',
|
||||||
|
'tour_delete',
|
||||||
|
'flight_view',
|
||||||
|
'flight_create',
|
||||||
|
'flight_edit',
|
||||||
|
'flight_delete'
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Traits;
|
||||||
|
|
||||||
|
class ActivityLog
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Traits;
|
||||||
|
|
||||||
|
use App\Models\ActivityLog;
|
||||||
|
|
||||||
|
trait LogsActivity
|
||||||
|
{
|
||||||
|
protected static function bootLogsActivity(): void
|
||||||
|
{
|
||||||
|
static::created(function ($model) {
|
||||||
|
$model->writeActivityLog('created', $model->getAttributes());
|
||||||
|
});
|
||||||
|
|
||||||
|
static::updated(function ($model) {
|
||||||
|
|
||||||
|
$changes = $model->getChanges();
|
||||||
|
|
||||||
|
unset($changes['updated_at']);
|
||||||
|
|
||||||
|
if (empty($changes)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$diff = [];
|
||||||
|
|
||||||
|
foreach ($changes as $key => $newValue) {
|
||||||
|
$diff[$key] = [
|
||||||
|
'old' => $model->getOriginal($key),
|
||||||
|
'new' => $newValue,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$model->writeActivityLog('updated', $diff);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function writeActivityLog(string $action, array $data): void
|
||||||
|
{
|
||||||
|
ActivityLog::create([
|
||||||
|
'admin_id' => auth('admin')->id(),
|
||||||
|
'model_type' => static::class,
|
||||||
|
'model_id' => $this->getKey(),
|
||||||
|
'action' => $action,
|
||||||
|
'changes' => $data,
|
||||||
|
'created_at' => now(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user