sort and pagination added
This commit is contained in:
parent
d98bc7bbb0
commit
9bcf1fb0eb
@ -14,7 +14,27 @@ class CategoryController extends Controller
|
||||
// نمایش لیست دستهبندیها با صفحهبندی
|
||||
public function index(Request $request)
|
||||
{
|
||||
$categories = Category::with('posts')->paginate(10);
|
||||
// گرفتن فیلتر و سورت از درخواست کاربر
|
||||
$filter = $request->input('filter'); // فیلتر (به عنوان مثال، نام دستهبندی)
|
||||
$sortBy = $request->input('sort_by', 'created_at'); // مرتبسازی (پیشفرض بر اساس زمان ایجاد)
|
||||
$sortDirection = $request->input('sort_direction', 'desc'); // جهت مرتبسازی (پیشفرض نزولی)
|
||||
$perPage = $request->input('per_page', 10); // تعداد آیتمها در هر صفحه (پیشفرض ۱۰)
|
||||
|
||||
// شروع کوئری با مدل دستهبندی و روابط
|
||||
$query = Category::with('posts');
|
||||
|
||||
// اعمال فیلتر در صورت موجود بودن
|
||||
if (!empty($filter)) {
|
||||
$query->where('name', 'like', '%' . $filter . '%');
|
||||
}
|
||||
|
||||
// اعمال مرتبسازی
|
||||
$query->orderBy($sortBy, $sortDirection);
|
||||
|
||||
// گرفتن نتایج با پجینیشن
|
||||
$categories = $query->paginate($perPage);
|
||||
|
||||
// بازگشت دادهها با استفاده از ریسورس
|
||||
return CategoryResource::collection($categories);
|
||||
}
|
||||
|
||||
@ -33,39 +53,66 @@ public function show($id)
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function getCategoryWithPosts(Request $request, $slug)
|
||||
{
|
||||
// یافتن دستهبندی بر اساس slug
|
||||
$category = Category::where('slug', $slug)->first();
|
||||
{
|
||||
// یافتن دستهبندی بر اساس slug
|
||||
$category = Category::where('slug', $slug)->first();
|
||||
|
||||
if (!$category) {
|
||||
return response()->json([
|
||||
'message' => 'دستهبندی مورد نظر یافت نشد.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
// دریافت تعداد پستها در هر صفحه (پیشفرض: ۱۰)
|
||||
$perPage = $request->input('per_page', 10);
|
||||
|
||||
// دریافت پستهای مربوط به این دستهبندی به ترتیب جدیدترینها
|
||||
$posts = $category->posts()->with(['category', 'tags', 'author'])
|
||||
->orderBy('published_at', 'desc')
|
||||
->paginate($perPage);
|
||||
|
||||
// بازگشت اطلاعات دستهبندی و پستها
|
||||
if (!$category) {
|
||||
return response()->json([
|
||||
'category' => new CategoryResource($category),
|
||||
'posts' => PostResource::collection($posts),
|
||||
'pagination' => [
|
||||
'total' => $posts->total(),
|
||||
'per_page' => $posts->perPage(),
|
||||
'current_page' => $posts->currentPage(),
|
||||
'last_page' => $posts->lastPage(),
|
||||
'from' => $posts->firstItem(),
|
||||
'to' => $posts->lastItem(),
|
||||
],
|
||||
], 200);
|
||||
'message' => 'دستهبندی مورد نظر یافت نشد.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
// دریافت تعداد پستها در هر صفحه (پیشفرض: ۱۰)
|
||||
$perPage = $request->input('per_page', 10);
|
||||
|
||||
// دریافت فیلتر و سورت از درخواست
|
||||
$filter = $request->input('filter'); // فیلتر (به عنوان مثال، عنوان پست)
|
||||
$sortBy = $request->input('sort_by', 'published_at'); // مرتبسازی (پیشفرض بر اساس تاریخ انتشار)
|
||||
$sortDirection = $request->input('sort_direction', 'desc'); // جهت مرتبسازی (پیشفرض نزولی)
|
||||
|
||||
// شروع کوئری با پستهای مربوط به دستهبندی
|
||||
$postsQuery = $category->posts()->with(['category', 'tags', 'author']);
|
||||
|
||||
// اعمال فیلتر در صورت موجود بودن
|
||||
if (!empty($filter)) {
|
||||
$postsQuery->where('title', 'like', '%' . $filter . '%');
|
||||
}
|
||||
|
||||
// اعمال مرتبسازی
|
||||
$postsQuery->orderBy($sortBy, $sortDirection);
|
||||
|
||||
// گرفتن پستها با پجینیشن
|
||||
$posts = $postsQuery->paginate($perPage);
|
||||
|
||||
// ساخت meta و links برای pagination
|
||||
$meta = [
|
||||
'total' => $posts->total(),
|
||||
'per_page' => $posts->perPage(),
|
||||
'current_page' => $posts->currentPage(),
|
||||
'last_page' => $posts->lastPage(),
|
||||
'from' => $posts->firstItem(),
|
||||
'to' => $posts->lastItem(),
|
||||
];
|
||||
|
||||
$links = [
|
||||
'first' => $posts->url(1),
|
||||
'last' => $posts->url($posts->lastPage()),
|
||||
'prev' => $posts->previousPageUrl(),
|
||||
'next' => $posts->nextPageUrl(),
|
||||
];
|
||||
|
||||
// بازگشت اطلاعات دستهبندی و پستها
|
||||
return response()->json([
|
||||
'category' => new CategoryResource($category),
|
||||
'posts' => PostResource::collection($posts),
|
||||
'meta' => $meta,
|
||||
'links' => $links,
|
||||
], 200);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ذخیره یک دستهبندی جدید
|
||||
public function store(Request $request)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user