php 如何使用多个要求规定产品

zd287kbt  于 2023-11-16  发布在  PHP
关注(0)|答案(1)|浏览(107)

我有一个问题,如何做规范性的分析,在产品的基础上,其评级,价格,位置,销售,和类别。
这是我的代码

$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);

x8diyxa7

x8diyxa71#

如果你主要想在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

字符串

相关问题