vendredi 5 avril 2019

how to sortby the result of countby data laravel

i need to view on blade most popular categories from products table by collecting products and make count of categories of them and sorting result to arrange them desc

    $popular_channels = products::select('category')->get();

    $collection = collect($popular_products);
    $counted = $collection->countBy(function ($value){
        return $value['category'];
    });

    $sorted = array_values(Arr::sort($counted, function ($values) {
        return $values['category'];
    }));
    $counted = json_decode($counted,JSON_UNESCAPED_UNICODE);

    $popular = channels::query();

    foreach ($counted as $key=>$value)
    {
        $popular = $popular->where('id', $key);
    }
    $popular = $popular->offset(0)->limit(3)->orderby('id','desc')->get();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire