Feature #2 : Cities
This commit is contained in:
42
app/Exports/CitiesExport.php
Normal file
42
app/Exports/CitiesExport.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?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\City;
|
||||
|
||||
class CitiesExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
|
||||
{
|
||||
public function collection(){
|
||||
return City::with('province')->get();
|
||||
}
|
||||
|
||||
public function map($row): array{
|
||||
return [
|
||||
$row->id,
|
||||
$row->code,
|
||||
$row->name,
|
||||
$row->province->name,
|
||||
$row->created_at
|
||||
];
|
||||
}
|
||||
public function headings(): array{
|
||||
return [
|
||||
'ID',
|
||||
'Code',
|
||||
'Name',
|
||||
'Province',
|
||||
'Created At'
|
||||
];
|
||||
}
|
||||
|
||||
public function columnFormats(): array{
|
||||
return [
|
||||
'A' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER,
|
||||
'E' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
|
||||
];
|
||||
}
|
||||
}
|
||||
135
app/Http/Controllers/CitiesController.php
Normal file
135
app/Http/Controllers/CitiesController.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Location\Exports\CitiesExport;
|
||||
use Modules\Location\Http\Requests\CityRequest;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\Province;
|
||||
|
||||
class CitiesController extends Controller
|
||||
{
|
||||
public $user;
|
||||
|
||||
public function index(){
|
||||
return view('location::cities.index');
|
||||
}
|
||||
|
||||
public function create(){
|
||||
$provinces = Province::all();
|
||||
return view('location::cities.create',compact('provinces'));
|
||||
}
|
||||
|
||||
public function store(CityRequest $request){
|
||||
$validate = $request->validated();
|
||||
|
||||
if($validate){
|
||||
try {
|
||||
City::create($validate);
|
||||
return redirect()->route('locations.cities.index')->with('success', 'City created successfully');
|
||||
} catch (\Exception $e) {
|
||||
return redirect()->route('locations.cities.create')->with('error', 'Failed to create city');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function edit($id){
|
||||
$city = City::find($id);
|
||||
$provinces = Province::all();
|
||||
return view('location::cities.create', compact('city', 'provinces'));
|
||||
}
|
||||
|
||||
public function update(CityRequest $request, $id){
|
||||
$validate = $request->validated();
|
||||
|
||||
if($validate){
|
||||
try {
|
||||
$city = City::find($id);
|
||||
$city->update($validate);
|
||||
return redirect()->route('locations.cities.index')->with('success', 'City updated successfully');
|
||||
} catch (\Exception $e) {
|
||||
return redirect()->route('locations.cities.edit', $id)->with('error', 'Failed to update city');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id){
|
||||
try {
|
||||
City::destroy($id);
|
||||
echo json_encode(['message' => 'City deleted successfully', 'success' => true]);
|
||||
} catch (\Exception $e) {
|
||||
echo json_encode(['message' => 'Failed to delete city', '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 = City::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('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('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()
|
||||
{
|
||||
return Excel::download(new CitiesExport, 'cities.xlsx');
|
||||
}
|
||||
}
|
||||
36
app/Http/Requests/CityRequest.php
Normal file
36
app/Http/Requests/CityRequest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CityRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$rules =[
|
||||
'province_code' => 'required|exists:provinces,code'
|
||||
];
|
||||
|
||||
if ($this->method() === 'PUT') {
|
||||
$rules['code'] = 'required|string|max:6|unique:cities,code,' . $this->id;
|
||||
$rules['name'] = 'required|string|max:2554|unique:cities,name,' . $this->id;
|
||||
} else {
|
||||
$rules['code'] = 'required|string|max:6|unique:cities,code';
|
||||
$rules['name'] = 'required|string|max:2554|unique:cities,name';
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
16
app/Models/City.php
Normal file
16
app/Models/City.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Location\Models;
|
||||
|
||||
class City extends Base
|
||||
{
|
||||
protected $fillable = ['province_code', 'code', 'name'];
|
||||
|
||||
public function province(){
|
||||
return $this->belongsTo(Province::class, 'province_code', 'code');
|
||||
}
|
||||
|
||||
public function districts(){
|
||||
return $this->hasMany(District::class, 'city_code', 'code');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user