thephpleague / fractal

Output complex, flexible, AJAX/RESTful data structures.

Home Page:fractal.thephpleague.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When sorted array enters fractal facade it becomes unsorted

OmarAbdelaziz646 opened this issue · comments

I try to get data from database then order them according to the sort type that is sent so I do that

public function getStoreProducts($store_id): array
    {
        $products = Product::with('translation', 'image', 'price', 'group.translation', 'group.subcategory.subCategoryTranslations','store')
            ->where('store_id',$store_id)->where('product_status', '=', 2)
            ->groupBy('group_id');
        if (request()->has('sort') && !empty(request()->get('sort'))) {
            if (request()->get('sort') === 'date') {
                $products = $products->orderBy('created_at', 'DESC');
            }elseif (request()->get('sort') == 'price'){
                $products = $products->whereHas('price')->with(array('price'=>function($query){
                    $query->orderBy('sell_price','asc');
                }));
            }elseif (request()->get('sort') == 'price-desc'){
                $products = $products->whereHas('price')->with(array('price'=>function($query){
                    $query->orderBy('sell_price','desc');
                }));
            }
        }


        return $this->toArray($products->paginate(15));
    }

so the data get sorted but when I pass them to the fractal transformer it got unsort so how can i prevent this operation

$products = $this->repository->getStoreProducts($store_id);

        $data = FractalFacade::collection($products["data"])
            ->transformWith(new ProductTransformer())
            ->parseIncludes(['translation', 'price', 'images', 'group.sub_category_translation', 'group.group_translation'])
            ->toArray();
        dd($data); 

But I noticed that this problem happens when the sort is on the relation So, for example
when I sort based on the created_at which is field in product the Products get out sort as it entered but when sort the data based on the price which is the relation with the products
so the entered sorted but it get out unsorted so How can I solve this problem

this has been open for a few years with no follow up so I will close it for now. if it is still an issue please feel free to reopen.