DEV Community

Cover image for Explain in 5 Levels of Difficulty
Maxi Contieri
Maxi Contieri

Posted on

Explain in 5 Levels of Difficulty

TL;DR: In this series, I will explain several concepts to five different audiences

Top comments (4)

Collapse
 
nevodavid profile image
Nevo David

love this approach, breaking things down by skill totally makes sense tbh - you think people tend to pick it up faster when they can actually start from their own level?

Collapse
 
dotallio profile image
Dotallio

Love how you break down such a huge range of topics into levels - makes even the scary concepts feel accessible. Any chance you'll do one on databases or APIs next?

Collapse
 
mcsee profile image
Maxi Contieri

sure! why not!

Collapse
 
kipyegon_festus profile image
Kipyegon Festus
public function index(Request $request): JsonResponse
{
    $perPage = max(1, min((int) ($request->per_page ?? 12), 100));
    $user = $request->user();

    $query = Product::query()
        ->with(['category', 'store'])
        ->withCount('inventories')
        ->withSum('inventories as total_stock', 'quantity');

    if (!$user->isAdmin()) {
        $query->whereIn('store_id', $this->service->allowedStoreIds($user));
    }

    $query
        ->when($request->store_id, function ($q, $storeId) use ($user) {
            $this->service->authorizeStoreAccess($user, $storeId);
            $q->where('store_id', $storeId);
        })
        ->when($request->search, function ($q, $search) {
            $search = str_replace(['%', '_'], ['\%', '\_'], trim($search));
            $q->where(function ($sub) use ($search) {
                $sub->where('product_name', 'like', "%{$search}%")
                    ->orWhere('sku', 'like', "%{$search}%");
            });
        })
        ->when($request->filled('category_ids'), function ($q) use ($request) {
            $ids = collect(
                is_array($request->category_ids)
                    ? $request->category_ids
                    : explode(',', (string) $request->category_ids)
            )->map(fn ($id) => (int) trim($id))->filter()->values()->all();

            if (!empty($ids)) $q->whereIn('category_id', $ids);
        })
        ->when(!$request->filled('category_ids') && $request->filled('category_id'), function ($q) use ($request) {
            $q->where('category_id', (int) $request->category_id);
        })
        ->when($request->has('is_active') && $request->is_active !== '', function ($q) use ($request) {
            $q->where('is_active', filter_var($request->is_active, FILTER_VALIDATE_BOOLEAN));
        })
        ->orderByDesc('product_id');

    $products = $query->paginate($perPage);

    return response()->json([
        'data' => $products->items(),
        'meta' => [
            'current_page' => $products->currentPage(),
            'last_page'    => $products->lastPage(),
            'per_page'     => $products->perPage(),
            'total'        => $products->total(),
            'from'         => $products->firstItem(),
            'to'           => $products->lastItem(),
        ],
    ]);
}working on laravel optimization. someone to check for me how i should optimize this code .
Enter fullscreen mode Exit fullscreen mode

Some comments may only be visible to logged-in visitors. Sign in to view all comments.