fix : complete Activity log in admin side
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\ActivityLog;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ActivityLogController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$logs = ActivityLog::query()
|
||||
->with('causer')
|
||||
->when($request->filled('log_name'), function ($query) use ($request) {
|
||||
$query->where('log_name', $request->input('log_name'));
|
||||
})
|
||||
->latest('created_at')
|
||||
->paginate(20);
|
||||
|
||||
return view('admin.activity-logs.index', compact('logs'));
|
||||
}
|
||||
}
|
||||
@@ -169,7 +169,7 @@ class FlightController extends Controller
|
||||
$fileId = $uploadedFile->id;
|
||||
}
|
||||
|
||||
Flight::create([
|
||||
$flight = Flight::create([
|
||||
'title' => $request->input('title'),
|
||||
'file_id' => $fileId,
|
||||
'airline_id' => $request->input('airline_id'),
|
||||
@@ -183,8 +183,12 @@ class FlightController extends Controller
|
||||
'views' => 0,
|
||||
'description' => $request->input('description'),
|
||||
'status' => $request->input('status'),
|
||||
'created_by' => auth('admin')->id(),
|
||||
'updated_by' => auth('admin')->id(),
|
||||
]);
|
||||
|
||||
logActivity('flights', $flight->id, 'created');
|
||||
|
||||
DB::commit();
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
@@ -227,6 +231,12 @@ class FlightController extends Controller
|
||||
|
||||
public function update(Request $request, Flight $flight)
|
||||
{
|
||||
$before = $flight->only([
|
||||
'title', 'airline_id', 'flight_type', 'departure_time',
|
||||
'departure_airport_id', 'arrival_airport_id', 'price',
|
||||
'summary', 'capacity', 'description', 'status',
|
||||
]);
|
||||
|
||||
$request->validate([
|
||||
'title' => [
|
||||
'required',
|
||||
@@ -327,6 +337,12 @@ class FlightController extends Controller
|
||||
'capacity' => $request->input('capacity'),
|
||||
'description' => $request->input('description'),
|
||||
'status' => $request->input('status'),
|
||||
'updated_by' => auth('admin')->id(),
|
||||
]);
|
||||
|
||||
logActivity('flights', $flight->id, 'updated', [
|
||||
'before' => $before,
|
||||
'after' => $flight->only(array_keys($before)),
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
@@ -357,6 +373,7 @@ class FlightController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
logActivity('flights', $flight->id, 'deleted');
|
||||
$flight->delete();
|
||||
|
||||
return redirect()->route('admin.flights.index')->withErrors([
|
||||
|
||||
@@ -107,28 +107,24 @@ class TourController extends Controller
|
||||
$fileId = $uploadedFile->id;
|
||||
}
|
||||
|
||||
Tour::create([
|
||||
$tour = Tour::create([
|
||||
'title' => $request->input('title'),
|
||||
|
||||
'summary' => $request->input('summary'),
|
||||
|
||||
'description' => $request->input('description'),
|
||||
|
||||
'origin' => $request->input('origin'),
|
||||
|
||||
'destination' => $request->input('destination'),
|
||||
|
||||
'file_id' => $fileId,
|
||||
|
||||
'duration' => $request->input('duration'),
|
||||
|
||||
'departure_time' => $request->input('departure_time'),
|
||||
|
||||
'price' => $request->input('price'),
|
||||
|
||||
'status' => $request->input('status'),
|
||||
'created_by' => auth('admin')->id(),
|
||||
'updated_by' => auth('admin')->id(),
|
||||
]);
|
||||
|
||||
logActivity('tours', $tour->id, 'created');
|
||||
|
||||
|
||||
DB::commit();
|
||||
|
||||
} catch (\Exception $exception) {
|
||||
@@ -160,9 +156,16 @@ class TourController extends Controller
|
||||
}
|
||||
|
||||
|
||||
public function update(tourUpdateRequest $request, Tour $tour) {
|
||||
public function update(tourUpdateRequest $request, Tour $tour)
|
||||
{
|
||||
try {
|
||||
|
||||
$before = $tour->only([
|
||||
'title', 'summary', 'description', 'origin',
|
||||
'destination', 'file_id', 'duration', 'price',
|
||||
'departure_time', 'status',
|
||||
]);
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$fileId = $tour->file_id;
|
||||
@@ -193,26 +196,21 @@ class TourController extends Controller
|
||||
|
||||
$tour->update([
|
||||
'title' => $request->input('title'),
|
||||
|
||||
'summary' => $request->input('summary'),
|
||||
|
||||
'description' => $request->input('description'),
|
||||
|
||||
'origin' => $request->input('origin'),
|
||||
|
||||
'destination' => $request->input('destination'),
|
||||
|
||||
'file_id' => $fileId,
|
||||
|
||||
'duration' => $request->input('duration'),
|
||||
|
||||
'views' => 0,
|
||||
|
||||
'departure_time' => $request->input('departure_time'),
|
||||
|
||||
'price' => $request->input('price'),
|
||||
|
||||
'status' => $request->input('status'),
|
||||
'updated_by' => auth('admin')->id(),
|
||||
]);
|
||||
|
||||
logActivity('tours', $tour->id, 'updated', [
|
||||
'before' => $before,
|
||||
'after' => $tour->only(array_keys($before)),
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
@@ -237,15 +235,16 @@ class TourController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function destroy(Tour $tour)
|
||||
{
|
||||
if ($tour->bookings()->exists()) {
|
||||
return back()->withErrors(['error' => 'این پرواز دارای رزرو است و قابل حذف نیست.']);
|
||||
}
|
||||
|
||||
logActivity('flights', $tour->id, 'deleted');
|
||||
|
||||
$tour->delete();
|
||||
|
||||
return redirect()
|
||||
->route('admin.tours.index')
|
||||
->withErrors([
|
||||
'success' => 'تور با موفقیت حذف شد'
|
||||
]);
|
||||
return redirect()->route('admin.flights.index')->withErrors(['success'=> 'پرواز با موفقیت حذف شد']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ class FlightController extends Controller
|
||||
|
||||
public function show(Flight $flight)
|
||||
{
|
||||
abort_if($flight->status !== FlightStatus::ENABLE->value, 404);
|
||||
abort_if($flight->status !== FlightStatus::ENABLE, 404);
|
||||
|
||||
$flight->increment('views');
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class TourController extends Controller
|
||||
|
||||
public function show(Tour $tour)
|
||||
{
|
||||
abort_if($tour->status !== TourStatus::ENABLE->value, 404);
|
||||
abort_if($tour->status !== TourStatus::ENABLE, 404);
|
||||
|
||||
$tour->increment('views');
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ActivityLog extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'log_name',
|
||||
'subject_id',
|
||||
'causer_id',
|
||||
'event',
|
||||
'changes',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'changes' => 'array',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($log) {
|
||||
$log->created_at = now();
|
||||
});
|
||||
}
|
||||
|
||||
public function causer()
|
||||
{
|
||||
return $this->belongsTo(Admin::class, 'causer_id');
|
||||
}
|
||||
}
|
||||
+8
-6
@@ -36,12 +36,14 @@ class File extends Model
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'extension',
|
||||
'size',
|
||||
'original_name',
|
||||
'path'
|
||||
];
|
||||
'name',
|
||||
'extension',
|
||||
'size',
|
||||
'original_name',
|
||||
'path',
|
||||
'created_by',
|
||||
'updated_by'
|
||||
];
|
||||
|
||||
public function flights()
|
||||
{
|
||||
|
||||
+59
-40
@@ -41,64 +41,73 @@ use Illuminate\Database\Eloquent\Model;
|
||||
*/
|
||||
class Flight extends Model
|
||||
{
|
||||
protected $table = 'flights';
|
||||
protected $table = 'flights';
|
||||
|
||||
protected $casts = [
|
||||
'file_id' => 'int',
|
||||
'airline_id' => 'int',
|
||||
'flight_type' => 'int',
|
||||
'departure_time' => 'datetime',
|
||||
'departure_airport_id' => 'int',
|
||||
'arrival_airport_id' => 'int',
|
||||
'price' => 'float',
|
||||
'capacity' => 'int',
|
||||
protected $casts = [
|
||||
'file_id' => 'int',
|
||||
'airline_id' => 'int',
|
||||
'flight_type' => 'int',
|
||||
'departure_time' => 'datetime',
|
||||
'departure_airport_id' => 'int',
|
||||
'arrival_airport_id' => 'int',
|
||||
'price' => 'float',
|
||||
'capacity' => 'int',
|
||||
'views' => 'int',
|
||||
'status' => FlightStatus::class
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'file_id',
|
||||
'airline_id',
|
||||
'flight_type',
|
||||
'departure_time',
|
||||
'departure_airport_id',
|
||||
'arrival_airport_id',
|
||||
'price',
|
||||
'summary',
|
||||
'capacity',
|
||||
'description',
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'file_id',
|
||||
'airline_id',
|
||||
'flight_type',
|
||||
'departure_time',
|
||||
'departure_airport_id',
|
||||
'arrival_airport_id',
|
||||
'price',
|
||||
'summary',
|
||||
'capacity',
|
||||
'description',
|
||||
'views',
|
||||
'status'
|
||||
];
|
||||
'status',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
public function airline()
|
||||
{
|
||||
return $this->belongsTo(Airline::class);
|
||||
}
|
||||
protected $hidden = [
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
public function airline()
|
||||
{
|
||||
return $this->belongsTo(Airline::class);
|
||||
}
|
||||
|
||||
public function file()
|
||||
{
|
||||
return $this->belongsTo(File::class);
|
||||
}
|
||||
public function file()
|
||||
{
|
||||
return $this->belongsTo(File::class);
|
||||
}
|
||||
|
||||
public function airport()
|
||||
{
|
||||
return $this->belongsTo(Airport::class, 'departure_airport_id');
|
||||
}
|
||||
public function airport()
|
||||
{
|
||||
return $this->belongsTo(Airport::class, 'departure_airport_id');
|
||||
}
|
||||
|
||||
public function bookings()
|
||||
{
|
||||
return $this->hasMany(Booking::class);
|
||||
}
|
||||
|
||||
public function bookings()
|
||||
{
|
||||
return $this->hasMany(Booking::class);
|
||||
}
|
||||
public function arrivalAirport()
|
||||
{
|
||||
return $this->belongsTo(Airport::class, 'arrival_airport_id');
|
||||
}
|
||||
|
||||
public function departureAirport()
|
||||
{
|
||||
return $this->belongsTo(Airport::class, 'departure_airport_id');
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(Comment::class, 'flight_id');
|
||||
@@ -113,4 +122,14 @@ class Flight extends Model
|
||||
$query->where('flight_type', $request->input('flight_type'));
|
||||
}
|
||||
}
|
||||
|
||||
public function creator()
|
||||
{
|
||||
return $this->belongsTo(Admin::class, 'created_by');
|
||||
}
|
||||
|
||||
public function updater()
|
||||
{
|
||||
return $this->belongsTo(Admin::class, 'updated_by');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ class Tour extends Model
|
||||
'departure_time',
|
||||
'status',
|
||||
'created_at',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
public function file()
|
||||
@@ -51,4 +53,19 @@ class Tour extends Model
|
||||
{
|
||||
return $this->hasMany(Comment::class, 'tour_id');
|
||||
}
|
||||
|
||||
protected $hidden = [
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
public function creator()
|
||||
{
|
||||
return $this->belongsTo(Admin::class, 'created_by');
|
||||
}
|
||||
|
||||
public function updater()
|
||||
{
|
||||
return $this->belongsTo(Admin::class, 'updated_by');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Models\ActivityLog;
|
||||
|
||||
if (!function_exists('getUserFullName')) {
|
||||
function getUserFullName(?\App\Models\User $user = null): string
|
||||
{
|
||||
@@ -55,4 +58,17 @@ if (!function_exists('getFileUrl')) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('logActivity')) {
|
||||
function logActivity(string $logName, int $subjectId, string $event, array $changes = []): void
|
||||
{
|
||||
ActivityLog::create([
|
||||
'log_name' => $logName,
|
||||
'subject_id' => $subjectId,
|
||||
'causer_id' => auth('admin')->id(),
|
||||
'event' => $event,
|
||||
'changes' => $changes,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user