如何使用多种要求开具产品处方

How to prescribe products using multiple requirements

提问人:Sequoia 提问时间:11/9/2023 最后编辑:Tim LewisSequoia 更新时间:11/10/2023 访问量:29

问:

我在如何根据产品的评级、价格、位置、销售和类别对产品进行规范性分析方面遇到了问题。

这是我的代码

$products = Products::select(
            'products.*',
            'sellers.shop_name',
            'products.status as status',
            'products.price',
            'sellers.barangay as seller_location',
            'reviews.star',
            'products.category'
        )
        ->where('products.stocks', '>', 0)
        ->where('products.status', 'Approved')
        ->where('product_name', 'like', "%$searchQuery%")
        // ->whereIn('sellers.barangay', $uniqueNearbyBarangays)
        ->join('sellers', 'products.sellerID', '=', 'sellers.id')
        ->join('reviews', 'products.id', '=', 'reviews.productID')
        ->join('addresses as seller_address', 'sellers.id', '=', 'seller_address.userID')
        ->join('addresses as product_address', 'products.id', '=', 'product_address.userID')
        ->orderBy('products.price')
        ->orderByDesc('reviews.star') 
        ->orderBy('seller_location')
        ->paginate(50);

因此,我首先将所有与产品相关的表连接起来,并使用 orderBy 对其进行排序。

我的第一个代码是我尝试使用数组手动分组彼此最近的地方,但它似乎不起作用,因为我仍然从随机位置获得随机产品。

$nearbyBarangaysGroups = [
            ["Dolores", "Juliana", "Del Pilar", "San Jose", "Santo Rosario", "San Nicolas", "Santo Niño", "Santa Lucia", "Magliman", "Santa Teresita", "San Agustin", "San Felipe"],
            ["Alasas", "Baliti", "Bulaon", "Calulut", "Dela Paz Norte", "Dela Paz Sur", "Del Carmen", "Del Rosario", "Lara", "Maimpis", "Pulung Bulo", "Lourdes", "Quebiawan", "Saguin", "Malino", "Malpitic", "Pandaras", "Panipuan", "San Isidro", "San Juan", "San Pedro Cutud"]
        ];
        
        $uniqueNearbyBarangays = collect($nearbyBarangaysGroups)->flatten()->unique()->toArray();
        
        $products = Products::select(
            'products.*',
            'sellers.shop_name',
            'products.status as status',
            'products.price',
            'sellers.barangay as seller_location',
            'reviews.star',
            'products.category'
        )
        ->where('products.stocks', '>', 0)
        ->where('products.status', 'Approved')
        ->where('product_name', 'like', "%$searchQuery%")
        ->whereIn('sellers.barangay', $uniqueNearbyBarangays)
        ->join('sellers', 'products.sellerID', '=', 'sellers.id')
        ->join('reviews', 'products.id', '=', 'reviews.productID')
        ->join('addresses as seller_address', 'sellers.id', '=', 'seller_address.userID')
        ->join('addresses as product_address', 'products.id', '=', 'product_address.userID')
        ->orderBy('products.price')
        ->orderByDesc('reviews.star') 
        ->orderBy('seller_location')
        ->paginate(50);
php mysql laravel 联接

评论

0赞 Vinay Pandey 11/14/2023
请添加有关您获得的输出类型以及预期输出的详细信息

答:

0赞 Mark West 11/9/2023 #1

如果您主要想在 seller_location 上订购,则应先调用该 orderBy。然后,它将按seller_location对所有记录进行排序,其次按价格排序,第三按审查排序。

->orderBy('seller_location')
->orderBy('products.price')
->orderByDesc('reviews.star') 

// Location 1, price: 5.00, review: 5 stars
// Location 1, price: 6.00, review: 5 stars
// Location 1, price: 6.00, review: 4 stars
// Location 2, price: 5.00, review: 5 stars
// Location 2, price: 6.00, review: 5 stars
// Location 2, price: 6.00, review: 4 stars