Feature #3 : Districts
This commit is contained in:
44
app/Exports/DistrictsExport.php
Normal file
44
app/Exports/DistrictsExport.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Modules\Location\Models\District;
|
||||
|
||||
class DistrictsExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
|
||||
{
|
||||
public function collection(){
|
||||
return District::with('city.province')->get();
|
||||
}
|
||||
|
||||
public function map($row): array{
|
||||
return [
|
||||
$row->id,
|
||||
$row->code,
|
||||
$row->name,
|
||||
$row->city->name,
|
||||
$row->city->province->name,
|
||||
$row->created_at
|
||||
];
|
||||
}
|
||||
public function headings(): array{
|
||||
return [
|
||||
'ID',
|
||||
'Code',
|
||||
'Name',
|
||||
'City',
|
||||
'Province',
|
||||
'Created At'
|
||||
];
|
||||
}
|
||||
|
||||
public function columnFormats(): array{
|
||||
return [
|
||||
'A' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER,
|
||||
'F' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -132,4 +132,8 @@ class CitiesController extends Controller
|
||||
{
|
||||
return Excel::download(new CitiesExport, 'cities.xlsx');
|
||||
}
|
||||
|
||||
public function getCitiesByProvinceId($id){
|
||||
return response()->json(City::where('province_code', $id)->get());
|
||||
}
|
||||
}
|
||||
|
||||
135
app/Http/Controllers/DistrictsController.php
Normal file
135
app/Http/Controllers/DistrictsController.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Location\Exports\DistrictsExport;
|
||||
use Modules\Location\Http\Requests\DistrictRequest;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\District;
|
||||
use Modules\Location\Models\Province;
|
||||
|
||||
class DistrictsController extends Controller
|
||||
{
|
||||
public $user;
|
||||
|
||||
public function index(){
|
||||
return view('location::districts.index');
|
||||
}
|
||||
|
||||
public function create(){
|
||||
$provinces = Province::all();
|
||||
return view('location::districts.create', compact('provinces'));
|
||||
}
|
||||
|
||||
public function store(DistrictRequest $request){
|
||||
$validate = $request->validated();
|
||||
|
||||
if($validate){
|
||||
try{
|
||||
$district = District::create($validate);
|
||||
return redirect()->route('locations.districts.index')->with('success', 'District created successfully');
|
||||
} catch(\Exception $e){
|
||||
return redirect()->back()->with('error', 'Failed to create district. '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id){
|
||||
$district = District::find($id);
|
||||
$provinces = Province::all();
|
||||
$cities = City::where('province_code', $district->province_code)->get();
|
||||
return view('location::districts.create', compact('district', 'provinces','cities'));
|
||||
}
|
||||
|
||||
public function update(DistrictRequest $request, $id){
|
||||
$validate = $request->validated();
|
||||
|
||||
if($validate){
|
||||
try{
|
||||
$district = District::find($id);
|
||||
$district->update($validate);
|
||||
return redirect()->route('locations.districts.index')->with('success', 'District updated successfully');
|
||||
} catch(\Exception $e){
|
||||
return redirect()->back()->with('error', 'Failed to update district. '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id){
|
||||
try{
|
||||
District::destroy($id);
|
||||
echo json_encode(['message' => 'District deleted successfully','success'=>true ]);
|
||||
} catch(\Exception $e){
|
||||
echo json_encode(['message' => 'Failed to delete District', 'success' => false]);
|
||||
}
|
||||
}
|
||||
|
||||
public function dataForDatatables(Request $request){
|
||||
if (is_null($this->user) || !$this->user->can('provinces.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view users.');
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
$query = District::query();
|
||||
|
||||
// Apply search filter if provided
|
||||
if ($request->has('search') && !empty($request->get('search'))) {
|
||||
$search = $request->get('search');
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('code', 'LIKE', "%$search%");
|
||||
$q->orWhere('name', 'LIKE', "%$search%");
|
||||
$q->orWhereRelation('city','name', 'LIKE', "%$search%");
|
||||
$q->orWhereRelation('city.province','name', 'LIKE', "%$search%");
|
||||
});
|
||||
}
|
||||
|
||||
// Apply sorting if provided
|
||||
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
||||
$order = $request->get('sortOrder');
|
||||
$column = $request->get('sortField');
|
||||
$query->orderBy($column, $order);
|
||||
}
|
||||
|
||||
// Get the total count of records
|
||||
$totalRecords = $query->count();
|
||||
|
||||
// Apply pagination if provided
|
||||
if ($request->has('page') && $request->has('size')) {
|
||||
$page = $request->get('page');
|
||||
$size = $request->get('size');
|
||||
$offset = ($page - 1) * $size; // Calculate the offset
|
||||
|
||||
$query->skip($offset)->take($size);
|
||||
}
|
||||
|
||||
// Get the filtered count of records
|
||||
$filteredRecords = $query->count();
|
||||
|
||||
// Get the data for the current page
|
||||
$cities = $query->with('city.province')->get();
|
||||
|
||||
// Calculate the page count
|
||||
$pageCount = ceil($totalRecords/$request->get('size'));
|
||||
|
||||
// Calculate the current page number
|
||||
$currentPage = 0 + 1;
|
||||
|
||||
// Return the response data as a JSON object
|
||||
return response()->json([
|
||||
'draw' => $request->get('draw'),
|
||||
'recordsTotal' => $totalRecords,
|
||||
'recordsFiltered' => $filteredRecords,
|
||||
'pageCount' => $pageCount,
|
||||
'page' => $currentPage,
|
||||
'totalCount' => $totalRecords,
|
||||
'data' => $cities,
|
||||
]);
|
||||
}
|
||||
|
||||
public function export(Request $request){
|
||||
return Excel::download(new DistrictsExport, 'districts.xlsx');
|
||||
}
|
||||
}
|
||||
40
app/Http/Requests/DistrictRequest.php
Normal file
40
app/Http/Requests/DistrictRequest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class DistrictRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules()
|
||||
: array
|
||||
{
|
||||
$rules = [
|
||||
'province_code' => 'required|exists:provinces,code',
|
||||
'city_code' => 'required|exists:cities,code',
|
||||
];
|
||||
|
||||
if ($this->method() === 'PUT') {
|
||||
$rules['code'] = 'required|string|max:6|unique:districts,code,' . $this->id;
|
||||
$rules['name'] = 'required|string|max:2554|unique:districts,name,' . $this->id;
|
||||
} else {
|
||||
$rules['code'] = 'required|string|max:6|unique:districts,code';
|
||||
$rules['name'] = 'required|string|max:2554|unique:districts,name';
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize()
|
||||
: bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
20
app/Models/District.php
Normal file
20
app/Models/District.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Modules\Location\Database\Factories\DistrictFactory;
|
||||
|
||||
class District extends Base
|
||||
{
|
||||
protected $fillable = ['province_code','city_code', 'code', 'name'];
|
||||
|
||||
public function city(){
|
||||
return $this->belongsTo(City::class, 'city_code', 'code');
|
||||
}
|
||||
|
||||
public function villages(){
|
||||
return $this->hasMany(Village::class, 'district_code', 'code');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user