Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Understanding this code snippet, debug a $query

I have the task of debugging some code that isn't mine and quite frankly I have no clue what is happening in this code. The situation is that this code works for everyone else except a specific provider and I don't know why, and I need to find out what is happening.

This is the method in my controller

public function postData(Request $request)
    {
        $query = Product::query();
        $query->whereHas('brand', function ($query) use($request) {
            $query->whereHas('vendor', function ($query) use($request) {
                $query->where('id', $request->id);
            });
        });

        if ($request->has('vendor_id')) {
            $country = session('country');

            $query->whereHas('countryProducts', function ($query) use ($country) {
                $query->where('country_id', $country['id']);
            });
            $query->with([
                'brand.vendor',
                'countryProducts' => function ($query) use ($country) {
                    $query->where('country_id', $country['id']);
                }
            ]);
        } else {
            $query->with([
                'brand.vendor'
            ]);
        }
        if ($request->has('range1')) {
            $query->whereHas('countryProducts', function ($query) use ($request) {
                $query->whereBetween('price', [$request->range1, $request->range2]);
            });
        }
        if ($request->brand) {
            $query->whereIn('brand_id', $request->brand);
        } else {
            $query->with([
                'brand.vendor'
            ]);
        }

        if ($request->size) {
            $query->whereHas('sizes', function ($query) use ($request) {
                $query->whereIn('id', $request->size);
            });
        }

        if ($request->has('term')) {
            $term = '%' . $request->term . '%';
            $query->where(function ($query) use ($term) {
                $query->where('code', 'like', $term)
                    ->orWhere('sku', 'like', $term)
                    ->orWhere('description_spanish', 'like', $term)
                    ->orWhereHas('brand', function ($query) use ($term) {
                        $query->where('name', 'like', $term);
                    })
                    ->orWhereHas('countryProducts', function ($query) use ($term) {
                        $query->where('price', 'like', $term);
                    });

            });
        }

        if ($request->has('sort')) {
            list($sortCol, $sortDir) = explode('|', $request->sort);
            if ($sortCol == 'price') {
                $country = session('country');
                $request->request->remove('sort');

                $query->select([
                    'products.*',
                    'country_product.price as price'
                ])
                    ->join('country_product', function ($join) use ($country) {
                        $join->on('products.id', '=', 'country_product.product_id')
                            ->where('country_product.country_id', $country['id']);
                    })
                    ->orderBy('price','asc');
            }
        }

        if ($request->has('paginate')) {
            return TableVue::arrayOf($request, $query);
        } else {
            return $query->get();
        }
    }

The TableVue arrayOf method

static function arrayOf(Request $request, Builder $query, array $searchField = []) {
        // handle sort option
        self::filters($request, $query, $searchField);
        $perPage = $request->has('per_page') ? (int) $request->per_page : null;
        $pagination = $query->paginate($perPage);
        $pagination->appends([
            'sort' => $request->sort,
            'filter' => $request->term,
            'per_page' => $request->per_page
        ]);

        return response()->json($pagination);
    }

This code is supposed to return a list of products for the provider but it's returning 0 when I know for a fact there are products to be returned (they're visible in the db).

This is the response in the browser console for the provider getting to products

Response {url: "/provider/data?page=1&paginate=true&per_page=25&vendor_id=5&range1=0&range2=7015&id=5", ok: true, status: 200, statusText: "OK", headers: Headers, …}
body: {current_page: 1, data: Array(0), first_page_url: "http://dev.sondel.com/provider/data?per_page=25&page=1", from: null, last_page: 1, …}
bodyText: "{"current_page":1,"data":[],"first_page_url":"http:\/\/dev.sondel.com\/provider\/data?per_page=25&page=1","from":null,"last_page":1,"last_page_url":"http:\/\/dev.sondel.com\/provider\/data?per_page=25&page=1","next_page_url":null,"path":"http:\/\/dev.sondel.com\/provider\/data","per_page":25,"prev_page_url":null,"to":null,"total":0}"
headers: Headers {map: {…}}
ok: true
status: 200
statusText: "OK"
url: "/provider/data?page=1&paginate=true&per_page=25&vendor_id=5&range1=0&range2=7015&id=5"
data: (...)
__proto__: Object

This is the response for a provider that is correctly displaying the products

body: {current_page: 1, data: Array(25), first_page_url: "http://dev.sondel.com/provider/data?per_page=25&page=1", from: 1, last_page: 60, …}
bodyText: "{"current_page":1,"data":[{"id":542,"code":"VTFGR","sku":"VTFGR", ..."
headers: Headers {map: {…}}
ok: true
status: 200
statusText: "OK"
url: "/provider/data?page=1&paginate=true&per_page=25&vendor_id=3&range1=0&range2=7015&id=3"
data: Object
__proto__: Object

I don't know where to start on figuring out why this is happening?

Any ideas?

0 likes
2 replies
jlrdw's avatar

Have you applied some of the filters like above.

Example the

if ($request->has('sort')) {

Maybe start commenting some of the code out and track down what works and what doesn't work and troubleshoot from there.

eggplantSword's avatar

great idea, I did comment out this part

$query->whereHas('brand', function ($query) use($request) {
    $query->whereHas('vendor', function ($query) use($request) {
        $query->where('id', $request->id);
    });
});

Now the provider does get all the products but I mean ALL the products, so this part is extremely impotant and when I add the brand then it doesn't work, it sends an empty array again, in this case there is only one brand for this provider so it should return all the products.

If I Log::info($request); then I get this info

 'size' => 
  array (
  ),
  'brand' => 
  array (
  ),
  'page' => '1',
  'paginate' => 'true',
  'per_page' => '25',
  'vendor_id' => '6',
  'range1' => '0',
  'range2' => '7015',
  'id' => '6',

Please or to participate in this conversation.