Filter projects by category id and expose category_id
Deploy Backend / deploy (push) Successful in 2m44s
Deploy Backend / deploy (push) Successful in 2m44s
This commit is contained in:
1 parent
eacd0d73f1
commit
c2960c4ed0
2 files changed
+58
-1
No files matched your search
@@ -21,6 +21,7 @@ class ProjectController extends Controller
|
||||
* @OA\Parameter(name="locale", in="path", required=true, @OA\Schema(type="string", enum={"fa","en"})),
|
||||
* @OA\Parameter(name="per_page", in="query", @OA\Schema(type="integer")),
|
||||
* @OA\Parameter(name="sort", in="query", @OA\Schema(type="string", enum={"asc","desc"})),
|
||||
* @OA\Parameter(name="category_id", in="query", @OA\Schema(type="integer")),
|
||||
* @OA\Response(response=200, description="OK")
|
||||
* )
|
||||
*/
|
||||
@@ -28,7 +29,21 @@ class ProjectController extends Controller
|
||||
{
|
||||
$perPage = request()->query('per_page', 10);
|
||||
|
||||
$projects = Project::with('category')->paginate($perPage);
|
||||
/**
|
||||
* The category tabs on the website filter by the category id instead of
|
||||
* its translated title: titles change with the locale and fall back to
|
||||
* English when a translation is missing, so matching on them silently
|
||||
* showed every project under every tab.
|
||||
*/
|
||||
$query = Project::with('category');
|
||||
|
||||
if ($categoryId = request()->query('category_id')) {
|
||||
$query->where('category_id', (int) $categoryId);
|
||||
}
|
||||
|
||||
$sort = request()->query('sort') === 'asc' ? 'asc' : 'desc';
|
||||
$projects = $query->orderBy('id', $sort)->paginate($perPage);
|
||||
|
||||
$formattedProjects = $projects->items();
|
||||
|
||||
$formattedProjects = array_map(function ($project) use ($locale) {
|
||||
@@ -36,6 +51,7 @@ class ProjectController extends Controller
|
||||
'id' => $project->id,
|
||||
'title' => $project->getTranslation('title', $locale),
|
||||
'category' => $project->category?->getTranslation('title', $locale),
|
||||
'category_id' => $project->category_id,
|
||||
'images' => [
|
||||
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
|
||||
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
|
||||
@@ -102,6 +118,7 @@ class ProjectController extends Controller
|
||||
'id' => $project->id,
|
||||
'title' => $project->getTranslation('title', $locale),
|
||||
'category' => $project->category?->getTranslation('title', $locale),
|
||||
'category_id' => $project->category_id,
|
||||
'images' => [
|
||||
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
|
||||
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
|
||||
|
||||
Reference in new issue
Block a user