简体   繁体   English

如何在Laravel中雄辩地获取每天,每周,每月和每年的同一记录的最新更新值?

[英]How to get last updated value of the same record for each day, week, month and year in Laravel eloquent?

This is my product_price table: 这是我的product_price表: 产品价格表 Notice that product_id 1 is updated multiple times on the same day. 请注意,product_id 1在同一天多次更新。 This is my current query: 这是我当前的查询:

$product = ProductPriceModel::where(['product_id' => $request->product_id])->distinct()->latest()->get()->groupBy(function ($date) {
                    return Carbon::parse($date->created_at)->format('Y-m-d');
                });

which give me this result: 这给我这个结果:

"data": {
    "productDetails": {
        "2018-09-17": [
            {
                "min_price": 0,
                "max_price": 1150,
                "price_diff": "425"
            },
            {
                "min_price": 100,
                "max_price": 200,
                "price_diff": "-945"
            }
        ],
        "2018-09-10": [
            {
                "min_price": 1070,
                "max_price": 1120,
                "price_diff": "-30"
            },
            {
                "min_price": 1100,
                "max_price": 1150,
                "price_diff": "15"
            },
            {
                "min_price": 1080,
                "max_price": 1140,
                "price_diff": "0"
            }
        ]
    }
}

What I expect it to return is only one record for each date. 我希望它返回的每个日期只有一个记录。 What changes needs to be done in above query to get exactly one record for each date? 为了在每个日期中获得一条准确的记录,在上面的查询中需要进行哪些更改?

If you want to get only one record you need to use first() in place of get() please give a try 如果您只想获取一条记录,则需要使用first()代替get()请尝试

$product = ProductPriceModel::where(['product_id' => $request->product_id])->distinct()->latest()->first()->groupBy(function ($date) {
                    return Carbon::parse($date->created_at)->format('Y-m-d');
                });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel Eloquent/MySQL 如何从每个父记录的最后一个条目中获取去年的记录? - Laravel Eloquent/MySQL how to get one last year records from last entry of each parent record? Laravel获得不同的/独特的日,周,月,年记录 - Laravel Get Distinct / Unique Records of Day, Week, Month, Year 在 MariaDb 查询中,给定年份、月份和星期,如何获取一周中的第一天的星期几? - How to get the day of month, of the first day of a week, given the year the month and the week in a MariaDb query? 如何通过输入月份和年份来获取 mysql 中月份的最后一天 - How to get last day of month in mysql by giving month and year as input 如何在mysql中获取每天的最后一条记录? - How to get last record of each day in mysql? 获取星期几,星期几,月份和年份的日期 - Get Date with Day of Week, Week Number, Month and Year 获取上周每一天的记录计数。 如果在特定日期没有找到记录,那么我想要 0 作为记录 - Get record count of each day of last week. if no record found in particular date then i want 0 as record 在MySQL中从最后一天,星期,月份和年份中获取数据 - Fetch data from last day, week, month and year in Mysql 如何在单个查询中基于日期范围(最后一天,上周,上个月,去年)从表中获取数据? - how to fetch data from table based on date range(last day, last week, last month,last year) in a single query? 如何计算最近/小时/天/周/月/年的访问量最大的页面? -PHP + MySQL - How to count most visited pages in last: hour / day / week / month / year ? - PHP+MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM