feat: Added StayType management with CRUD operations and DataTables integration

This commit is contained in:
2025-08-14 17:58:55 +05:45
parent 7771d57642
commit dee5d001b1
9 changed files with 369 additions and 0 deletions

View File

@@ -0,0 +1,153 @@
<?php
namespace Modules\CostCalculator\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Modules\CostCalculator\Models\StayType;
use Modules\CostCalculator\Services\StayTypeService;
use Yajra\DataTables\Facades\DataTables;
class StayTypeController extends Controller
{
protected $stayTypeService;
public function __construct(StayTypeService $stayTypeService)
{
$this->stayTypeService = $stayTypeService;
}
/**
* Display a listing of the resource.
*/
public function index(?int $id = null)
{
$isEditing = !is_null($id);
$stay = $isEditing ? $this->stayTypeService->getStayTypeById($id) : null;
if (request()->ajax()) {
$model = StayType::query()->orderBy('order');
return DataTables::eloquent($model)
->addIndexColumn()
->setRowClass('tableRow')
->editColumn('status', function (StayType $stay) {
$status = $stay->status ? 'Published' : 'Draft';
$color = $stay->status ? 'text-success' : 'text-danger';
return "<p class='{$color}'>{$status}</p>";
})
->addColumn('action', 'costcalculator::staytype.datatable.action')
->rawColumns(['action', 'status'])
->toJson();
}
return view('costcalculator::staytype.index', [
'stay' => $stay,
'editable' => $isEditing ? true : false,
'title' => $isEditing ? 'Edit Stay Type' : 'Add Stay Type',
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$isEditing = $request->has('id');
$request->merge([
'slug' => Str::slug($request->title),
]);
if ($isEditing) {
$validated = $request->validate([
'title' => ['required', 'string', 'max:255'],
'slug' => ['required', 'string'],
]);
$stay = $this->stayTypeService->updateStayType($request->id, partnerData: $validated);
flash()->success("stay type for {$stay->title} has been updated.");
return to_route('stay.index');
}
$maxOrder = StayType::max('order');
$order = $maxOrder ? ++$maxOrder : 1;
$request->mergeIfMissing([
'order' => $order,
]);
$validated = $request->validate([
'title' => ['required', 'string'],
'slug' => ['required', 'string'],
'order' => ['integer'],
]);
$stay = $this->stayTypeService->storeStayType($validated);
flash()->success("Stay for {$stay->title} has been created.");
return to_route('stay.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$partner = $this->stayTypeService->deleteStayType($id);
return response()->json(['status' => 200, 'message' => "Stay Type has been deleted."], 200);
}
public function reorder(Request $request)
{
$stays = $this->stayTypeService->getAllCategories();
foreach ($stays as $partner) {
foreach ($request->order as $order) {
if ($order['id'] == $partner->id) {
$partner->update(['order' => $order['position']]);
}
}
}
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
}
public function toggle($id)
{
$partner = StayType::findOrFail($id);
$partner->update(['status' => !$partner->status]);
return response(['status' => 200, 'message' => 'Toggled successfully'], 200);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Modules\CostCalculator\Models;
use App\Traits\CreatedUpdatedBy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\CCMS\Traits\UpdateCustomFields;
class StayType extends Model
{
use HasFactory, UpdateCustomFields, CreatedUpdatedBy;
protected $fillable = [
'title',
'slug',
'status',
'createdby',
'updatedby',
'order',
];
protected function casts(): array
{
return [
'custom' => 'array',
];
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Modules\CostCalculator\Services;
use Illuminate\Support\Facades\DB;
use Modules\CostCalculator\Models\StayType;
class StayTypeService
{
public function getAllCategories()
{
$query = StayType::query();
return $query->get();
}
public function storeStayType(array $partnerData): StayType
{
return DB::transaction(function () use ($partnerData) {
$partner = StayType::create($partnerData);
return $partner;
});
}
public function getStayTypeById(int $id)
{
return StayType::findOrFail($id);
}
public function updateStayType(int $id, array $partnerData)
{
$partner = $this->getStayTypeById($id);
return DB::transaction(function () use ($partner, $partnerData) {
$partner->update($partnerData);
return $partner;
});
}
public function deleteStayType(int $id)
{
return DB::transaction(function () use ($id) {
$partner = $this->getStayTypeById($id);
$partner->delete();
return true;
});
}
}

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('stay_types', function (Blueprint $table) {
$table->id();
$table->text('title');
$table->text('slug')->nullable();
$table->integer('status')->default(1);
$table->integer('createdby')->unsigned()->nullable();
$table->integer('updatedby')->unsigned()->nullable();
$table->integer('order')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('stay_types');
}
};

View File

@@ -0,0 +1,22 @@
{{ html()->form('POST', route('stay.store'))->class('needs-validation')->attributes(['novalidate'])->open() }}
@isset($stay)
{{ html()->hidden('id', $stay->id) }}
@endisset
<div class="card-body">
<div class="row">
<div class="col-sm-12">
<div class="mb-3">
{{ html()->label('Title')->for('title') }}
{{ html()->span('*')->class('text-danger') }}
{{ html()->text('title')->value($stay->title ?? old('title'))->class('form-control')->placeholder('Enter Title')->required() }}
{{ html()->div('Please enter a title.')->class('invalid-feedback') }}
</div>
</div>
<x-form-buttons :href="route('stay.index')" :label="isset($stay) ? 'Update' : 'Create'" />
</div>
</div>
{{ html()->form()->close() }}

View File

@@ -0,0 +1,11 @@
<div class="hstack flex-wrap gap-3">
<a href="{{ route('stay.index', $id) }}" class="link-success fs-15 edit-item-btn"><i class="ri-edit-2-line"></i></a>
<a data-link="{{ route('stay.toggle', $id) }}" data-bs-toggle="tooltip" data-bs-placement="bottom"
data-bs-title="Toggle" data-status="{{ $status == 1 ? 'Draft' : 'Published' }}"
class="link-info fs-15 toggle-item"><i class="{{ $status == 1 ? 'ri-toggle-line' : 'ri-toggle-fill' }}"></i></a>
<a href="javascript:void(0);" data-link="{{ route('stay.destroy', $id) }}" class="link-danger fs-15 remove-item"><i
class="ri-delete-bin-line"></i>
</a>
</div>

View File

@@ -0,0 +1,47 @@
@extends('layouts.app')
@section('content')
<div class="container-fluid">
<x-dashboard.breadcumb :title="$title" />
@if ($errors->any())
<x-flash-message type="danger" :messages="$errors->all()" />
@endif
<div class="row">
<div class="col-lg-4 col-xl-3">
<div class="card profile-card">
@include('costcalculator::staytype.add-stay-type-form')
</div>
</div>
<div class="col-lg-xl-8 col-lg-9">
<div class="card">
<div class="card-body">
@php
$columns = [
[
'title' => 'S.N',
'data' => 'DT_RowIndex',
'name' => 'DT_RowIndex',
'orderable' => false,
'searchable' => false,
'sortable' => false,
],
['title' => 'Name', 'data' => 'title', 'name' => 'title'],
['title' => 'Status', 'data' => 'status', 'name' => 'status'],
[
'title' => 'Action',
'data' => 'action',
'orderable' => false,
'searchable' => false,
],
];
@endphp
<x-data-table-script :route="route('stay.index')" :reorder="route('stay.reorder')" :columns="$columns" />
</div>
</div>
</div>
</div>
</div>
@endsection

View File

@@ -2,6 +2,7 @@
use Illuminate\Support\Facades\Route;
use Modules\CostCalculator\Http\Controllers\CostCalculatorController;
use Modules\CostCalculator\Http\Controllers\StayTypeController;
/*
|--------------------------------------------------------------------------
@@ -16,4 +17,9 @@ use Modules\CostCalculator\Http\Controllers\CostCalculatorController;
Route::group(['middleware' => ['web', 'auth', 'permission'], 'prefix' => 'admin/'], function () {
Route::resource('cost-calculator', CostCalculatorController::class)->names('cost');
Route::post('stay/reorder', [StayTypeController::class, 'reorder'])->name('stay.reorder');
Route::get('stay/toggle/{id}', [StayTypeController::class, 'toggle'])->name('stay.toggle');
Route::get('stay/{id?}', [StayTypeController::class, 'index'])->name('stay.index');
Route::resource('stay-type', StayTypeController::class)->names('stay')->only(['store', 'edit', 'destroy']);
});

View File

@@ -226,6 +226,24 @@ return [
],
],
[
'text' => 'Cost Calculator',
'icon' => 'ri-newspaper-line',
'module' => 'CostCalculator',
'submenu' => [
[
'url' => 'admin/stay',
'text' => 'Stay Type',
'can' => ['stay.index'],
],
[
'url' => 'admin/cost-calculator',
'text' => 'Cost Calculation',
'can' => ['cost.index'],
],
],
],
[
'text' => 'Documents',
'url' => 'admin/documents',