feat(Category): add category
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\JudicialPrecedentCategory;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class JudicialPrecedentCategoryController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = JudicialPrecedentCategory::query();
|
||||
|
||||
if ($request->filled('q')) {
|
||||
$q = $request->q;
|
||||
$query->where('name', 'like', "%{$q}%");
|
||||
}
|
||||
|
||||
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
||||
$categories = $query->paginate($perPage)->withQueryString();
|
||||
|
||||
return view('admin.judicial-precedent-category.index', compact('categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('admin.judicial-precedent-category.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|unique:judicial_precedent_categories,name'
|
||||
]);
|
||||
|
||||
JudicialPrecedentCategory::create($validated);
|
||||
|
||||
return redirect(route('judicial-precedent-category.index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(JudicialPrecedentCategory $judicialPrecedentCategory)
|
||||
{
|
||||
return view('admin.judicial-precedent-category.update', compact('judicialPrecedentCategory'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, JudicialPrecedentCategory $judicialPrecedentCategory)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|unique:judicial_precedent_categories,name,' . $judicialPrecedentCategory->id
|
||||
]);
|
||||
|
||||
$judicialPrecedentCategory->update($validated);
|
||||
|
||||
return redirect(route('judicial-precedent-category.edit', $judicialPrecedentCategory->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(JudicialPrecedentCategory $judicialPrecedentCategory)
|
||||
{
|
||||
$judicialPrecedentCategory->delete();
|
||||
return redirect(route('judicial-precedent-category.index'));
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Art;
|
||||
use App\Models\JudicialPrecedent;
|
||||
use App\Models\JudicialPrecedentCategory;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class JudicialPrecedentController extends Controller
|
||||
@@ -31,7 +32,8 @@ class JudicialPrecedentController extends Controller
|
||||
public function create()
|
||||
{
|
||||
$arts = Art::all();
|
||||
return view('admin.judicial-precedent.create', compact('arts'));
|
||||
$categories = JudicialPrecedentCategory::all();
|
||||
return view('admin.judicial-precedent.create', compact('arts', 'categories'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
@@ -42,6 +44,7 @@ class JudicialPrecedentController extends Controller
|
||||
'subject' => 'required|string',
|
||||
'full_text' => 'required|string',
|
||||
'issuing_authority' => 'nullable|string',
|
||||
'category_id' => 'nullable|exists:judicial_precedent_categories,id',
|
||||
'art_ids' => 'nullable|array',
|
||||
'art_ids.*' => 'exists:arts,id'
|
||||
]);
|
||||
@@ -51,7 +54,8 @@ class JudicialPrecedentController extends Controller
|
||||
'ruling_date' => $validated['ruling_date'],
|
||||
'subject' => $validated['subject'],
|
||||
'full_text' => $validated['full_text'],
|
||||
'issuing_authority' => $validated['issuing_authority'] ?? 'هیأت عمومی دیوان عالی کشور'
|
||||
'issuing_authority' => $validated['issuing_authority'] ?? 'هیأت عمومی دیوان عالی کشور',
|
||||
'category_id' => $validated['category_id']
|
||||
]);
|
||||
|
||||
if (!empty($validated['art_ids'])) {
|
||||
@@ -64,9 +68,10 @@ class JudicialPrecedentController extends Controller
|
||||
public function edit(JudicialPrecedent $judicialPrecedent)
|
||||
{
|
||||
$arts = Art::all();
|
||||
$categories = JudicialPrecedentCategory::all();
|
||||
$selectedArtIds = $judicialPrecedent->arts->pluck('id')->toArray();
|
||||
|
||||
return view('admin.judicial-precedent.update', compact('judicialPrecedent', 'arts', 'selectedArtIds'));
|
||||
return view('admin.judicial-precedent.update', compact('judicialPrecedent', 'arts', 'categories', 'selectedArtIds'));
|
||||
}
|
||||
|
||||
public function update(Request $request, JudicialPrecedent $judicialPrecedent)
|
||||
@@ -77,6 +82,7 @@ class JudicialPrecedentController extends Controller
|
||||
'subject' => 'required|string',
|
||||
'full_text' => 'required|string',
|
||||
'issuing_authority' => 'nullable|string',
|
||||
'category_id' => 'nullable|exists:judicial_precedent_categories,id',
|
||||
'art_ids' => 'nullable|array',
|
||||
'art_ids.*' => 'exists:arts,id'
|
||||
]);
|
||||
|
||||
@@ -14,7 +14,8 @@ class JudicialPrecedent extends Model
|
||||
'ruling_date',
|
||||
'subject',
|
||||
'full_text',
|
||||
'issuing_authority'
|
||||
'issuing_authority',
|
||||
'category_id'
|
||||
];
|
||||
|
||||
protected $hidden = ['created_at', 'updated_at'];
|
||||
@@ -23,4 +24,9 @@ class JudicialPrecedent extends Model
|
||||
{
|
||||
return $this->belongsToMany(Art::class, 'art_judicial_precedent');
|
||||
}
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(JudicialPrecedentCategory::class, 'category_id');
|
||||
}
|
||||
}
|
||||
|
||||
18
app/Models/JudicialPrecedentCategory.php
Normal file
18
app/Models/JudicialPrecedentCategory.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class JudicialPrecedentCategory extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name'];
|
||||
|
||||
public function judicialPrecedents()
|
||||
{
|
||||
return $this->hasMany(JudicialPrecedent::class, 'category_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user