feat: add province and city management
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\CityStoreRequest;
|
||||
use App\Http\Requests\CityUpdateRequest;
|
||||
use App\Models\City;
|
||||
use App\Models\Province;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CityController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = City::with('province');
|
||||
if ($request->filled('province_id')) {
|
||||
$query->where('province_id', $request->province_id);
|
||||
}
|
||||
$cities = $query->paginate(20);
|
||||
$provinces = Province::all();
|
||||
return view('admin.cities.index', compact('cities', 'provinces'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$provinces = Province::all();
|
||||
return view('admin.cities.create', compact('provinces'));
|
||||
}
|
||||
|
||||
// استفاده از CityRequest به جای Request
|
||||
public function store(CityStoreRequest $request)
|
||||
{
|
||||
City::create($request->validated());
|
||||
return redirect()->route('admin.locations.cities.index')->with('success', 'شهر با موفقیت ایجاد شد.');
|
||||
}
|
||||
|
||||
public function edit(City $city)
|
||||
{
|
||||
$provinces = Province::all();
|
||||
return view('admin.cities.edit', compact('city', 'provinces'));
|
||||
}
|
||||
|
||||
public function update(CityUpdateRequest $request, City $city)
|
||||
{
|
||||
$city->update($request->validated());
|
||||
return redirect()->route('admin.locations.cities.index')->with('success', 'شهر ویرایش شد.');
|
||||
}
|
||||
|
||||
public function destroy(City $city)
|
||||
{
|
||||
$city->delete();
|
||||
return back()->with('success', 'شهر حذف شد.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ProvinceUpdateRequest;
|
||||
use App\Models\Province;
|
||||
|
||||
class ProvinceController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$provinces = Province::paginate(20);
|
||||
return view('admin.provinces.index', compact('provinces'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('admin.provinces.create');
|
||||
}
|
||||
|
||||
|
||||
public function store(ProvinceUpdateRequest $request)
|
||||
{
|
||||
Province::create($request->validated());
|
||||
return redirect()->route('admin.locations.provinces.index')->with('success', 'استان با موفقیت ایجاد شد.');
|
||||
}
|
||||
|
||||
public function edit(Province $province)
|
||||
{
|
||||
return view('admin.provinces.edit', compact('province'));
|
||||
}
|
||||
|
||||
public function update(ProvinceUpdateRequest $request, Province $province)
|
||||
{
|
||||
$province->update($request->validated());
|
||||
return redirect()->route('admin.locations.provinces.index')->with('success', 'استان ویرایش شد.');
|
||||
}
|
||||
|
||||
public function destroy(Province $province)
|
||||
{
|
||||
$province->delete();
|
||||
return back()->with('success', 'استان حذف شد.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CityStoreRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' =>
|
||||
[
|
||||
'required',
|
||||
'string',
|
||||
'max:100'
|
||||
],
|
||||
'province_id' =>
|
||||
[
|
||||
'required',
|
||||
'exists:provinces,id'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ProvinceUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' =>
|
||||
[
|
||||
'required',
|
||||
'string',
|
||||
'max:100'
|
||||
],
|
||||
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
@extends('admin.layouts.app')
|
||||
|
||||
@section('title', 'افزودن شهر')
|
||||
|
||||
@section('content')
|
||||
<div class="admin-card p-4">
|
||||
<h5 class="mb-4">افزودن شهر جدید</h5>
|
||||
<form action="{{ route('admin.locations.cities.store') }}" method="POST">
|
||||
@csrf
|
||||
<div class="mb-3">
|
||||
<label class="form-label">نام شهر</label>
|
||||
<input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
|
||||
value="{{ old('name') }}">
|
||||
@error('name')
|
||||
<div class="invalid-feedback">{{ $message }}</div> @enderror
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">استان</label>
|
||||
<select name="province_id" class="form-control @error('province_id') is-invalid @enderror">
|
||||
<option value="">انتخاب استان...</option>
|
||||
@foreach($provinces as $province)
|
||||
<option
|
||||
value="{{ $province->id }}" {{ old('province_id') == $province->id ? 'selected' : '' }}>{{ $province->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@error('province_id')
|
||||
<div class="invalid-feedback">{{ $message }}</div> @enderror
|
||||
</div>
|
||||
<button type="submit" class="btn btn-success">ذخیره اطلاعات</button>
|
||||
<a href="{{ route('admin.locations.cities.index') }}" class="btn btn-secondary">انصراف</a>
|
||||
</form>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
@extends('admin.layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="card">
|
||||
<div class="card-header"><h5>ویرایش شهر: {{ $city->name }}</h5></div>
|
||||
<div class="card-body">
|
||||
<form action="{{ route('admin.locations.cities.update', $city->id) }}" method="POST">
|
||||
@csrf @method('PUT')
|
||||
<div class="form-group">
|
||||
<label>نام شهر</label>
|
||||
<input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
|
||||
value="{{ old('name', $city->name) }}">
|
||||
@error('name') <span class="text-danger">{{ $message }}</span> @enderror
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>استان مربوطه</label>
|
||||
<select name="province_id" class="form-control @error('province_id') is-invalid @enderror">
|
||||
@foreach($provinces as $province)
|
||||
<option
|
||||
value="{{ $province->id }}" {{ old('province_id', $city->province_id) == $province->id ? 'selected' : '' }}>
|
||||
{{ $province->name }}
|
||||
</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@error('province_id') <span class="text-danger">{{ $message }}</span> @enderror
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">بروزرسانی</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -0,0 +1,69 @@
|
||||
@extends('admin.layouts.app')
|
||||
|
||||
@section('title', 'مدیریت شهرها')
|
||||
|
||||
@section('breadcrumb')
|
||||
<div>
|
||||
<h1 class="page-title fw-medium fs-18 mb-2">مدیریت شهرها</h1>
|
||||
<nav>
|
||||
<ol class="breadcrumb mb-0">
|
||||
<li class="breadcrumb-item"><a href="{{ route('admin.dashboard.index') }}">داشبورد</a></li>
|
||||
<li class="breadcrumb-item active">مدیریت مناطق / شهرها</li>
|
||||
</ol>
|
||||
</nav>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@section('content')
|
||||
<div class="container-fluid pt-4">
|
||||
<div class="card custom-card">
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
<h5 class="mb-0">لیست شهرها</h5>
|
||||
<a href="{{ route('admin.locations.cities.create') }}" class="btn btn-primary">افزودن شهر جدید</a>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form action="{{ route('admin.locations.cities.index') }}" method="GET" class="mb-4">
|
||||
<select name="province_id" class="form-control w-25" onchange="this.form.submit()">
|
||||
<option value="">همه استانها</option>
|
||||
@foreach($provinces as $province)
|
||||
<option
|
||||
value="{{ $province->id }}" {{ request('province_id') == $province->id ? 'selected' : '' }}>{{ $province->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</form>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table class="table table-bordered table-hover mb-0">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>نام شهر</th>
|
||||
<th>استان</th>
|
||||
<th class="text-center">عملیات</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($cities as $city)
|
||||
<tr>
|
||||
<td>{{ $city->name }}</td>
|
||||
<td>{{ $city->province->name ?? '-' }}</td>
|
||||
<td class="text-center">
|
||||
<a href="{{ route('admin.locations.cities.edit', $city->id) }}"
|
||||
class="btn btn-sm btn-info text-white">ویرایش</a>
|
||||
<form action="{{ route('admin.locations.cities.destroy', $city->id) }}"
|
||||
method="POST" class="d-inline">
|
||||
@csrf @method('DELETE')
|
||||
<button class="btn btn-sm btn-danger" onclick="return confirm('آیا مطمئنید؟')">
|
||||
حذف
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="mt-3">{{ $cities->links() }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -0,0 +1,20 @@
|
||||
@extends('admin.layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="card">
|
||||
<div class="card-header"><h5>افزودن استان جدید</h5></div>
|
||||
<div class="card-body">
|
||||
<form action="{{ route('admin.locations.provinces.store') }}" method="POST">
|
||||
@csrf
|
||||
<div class="form-group">
|
||||
<label>نام استان</label>
|
||||
<input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
|
||||
value="{{ old('name') }}">
|
||||
@error('name') <span class="text-danger">{{ $message }}</span> @enderror
|
||||
</div>
|
||||
<button type="submit" class="btn btn-success">ذخیره</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
@extends('admin.layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="card">
|
||||
<div class="card-header"><h5>ویرایش استان: {{ $province->name }}</h5></div>
|
||||
<div class="card-body">
|
||||
<form action="{{ route('admin.locations.provinces.update', $province->id) }}" method="POST">
|
||||
@csrf @method('PUT')
|
||||
<div class="form-group">
|
||||
<label>نام استان</label>
|
||||
<input type="text" name="name" class="form-control @error('name') is-invalid @enderror"
|
||||
value="{{ old('name', $province->name) }}">
|
||||
@error('name') <span class="text-danger">{{ $message }}</span> @enderror
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">بروزرسانی</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -0,0 +1,59 @@
|
||||
@extends('admin.layouts.app')
|
||||
|
||||
@section('title', 'مدیریت استانها')
|
||||
|
||||
@section('breadcrumb')
|
||||
<div>
|
||||
<h1 class="page-title fw-medium fs-18 mb-2">مدیریت استانها</h1>
|
||||
<nav>
|
||||
<ol class="breadcrumb mb-0">
|
||||
<li class="breadcrumb-item"><a href="{{ route('admin.dashboard.index') }}">داشبورد</a></li>
|
||||
<li class="breadcrumb-item active">مدیریت مناطق / استانها</li>
|
||||
</ol>
|
||||
</nav>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@section('content')
|
||||
<div class="container-fluid pt-4">
|
||||
<div class="card custom-card">
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
<h5 class="mb-0">لیست استانها</h5>
|
||||
<a href="{{ route('admin.locations.provinces.create') }}" class="btn btn-primary">افزودن استان جدید</a>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-bordered table-hover mb-0">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>شناسه</th>
|
||||
<th>نام استان</th>
|
||||
<th class="text-center">عملیات</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($provinces as $province)
|
||||
<tr>
|
||||
<td>{{ $province->id }}</td>
|
||||
<td>{{ $province->name }}</td>
|
||||
<td class="text-center">
|
||||
<a href="{{ route('admin.locations.provinces.edit', $province->id) }}"
|
||||
class="btn btn-sm btn-info text-white">ویرایش</a>
|
||||
<form action="{{ route('admin.locations.provinces.destroy', $province->id) }}"
|
||||
method="POST" class="d-inline">
|
||||
@csrf @method('DELETE')
|
||||
<button class="btn btn-sm btn-danger" onclick="return confirm('آیا مطمئنید؟')">
|
||||
حذف
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="mt-3">{{ $provinces->links() }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
Reference in New Issue
Block a user