【问题标题】:Get the latest data from database从数据库中获取最新数据
【发布时间】:2017-09-28 16:05:56
【问题描述】:

我需要从表中获取最新数据。我在 URL 中传递了日期,并使用它进行过滤。所以,我有这样的事情:

$latest = Latest::select('s_customer', 's_product', 's_starttermin')
        ->leftJoin('temp_cat', 'temp_cat.spotid', '=', 'mc_spots.spotid')
        ->where('s_active', '=', 1)
        ->orderBy('s_starttermin', 'desc')
        ->limit(50);

if (!empty(Input::get('date'))) {
    $latest = $latest->where('s_starttermin', '=', Input::get('date'));
}

例如,今天我没有2017-09-28 的任何最新产品。在这种情况下,$latest 是一个空数组。最后一个是三天前。如何仅获取最新数据?提醒:我过滤使用s_starttermin...

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    我创建了一个函数来调用它,直到我们找到我们的最新记录,但要小心,如果你没有任何记录,它会循环直到无限,我会添加一些 $opportunities 变量或其他东西:

        public function getLatest($date){
            $latest = Latest::select('s_customer', 's_product', 's_starttermin')
                        ->leftJoin('temp_cat', 'temp_cat.spotid', '=', 'mc_spots.spotid')
                        ->where('s_active', '=', 1)->orderBy('s_starttermin', 'desc')->limit(50);
    
            if (!empty($date)) {
                $latest = $latest->where('s_starttermin', '=', Input::get('date'));
            }
    
           $latest = $latest->get();
    
           if(empty($latest)){
              return $this->getLatest(Carbon::parse($date)->subDay());
           }else{
             return $latest;
           }
         }
    

    把你的Input::get('date')传给他

    $this->getLatest(Input::get('date'))
    

    【讨论】:

    • 但我必须得到指定日期的结果。这就是为什么我的表单中有过滤日期,它作为Input::get('date') 传递。如果今天没有结果,我需要显示昨天的最新结果(例如)
    • 你误解了我的意思。这将显示小于今天日期的所有结果。我只想显示指定日期。如果2017-09-28 没有结果并且最后一个来自2017-09-27,那么我想显示来自2017-09-27 的所有结果
    【解决方案2】:

    也许这会对你有所帮助..

    $latest = Latest::select('s_customer', 's_product', 's_starttermin')
                ->leftJoin('temp_cat', 'temp_cat.spotid', '=', 'mc_spots.spotid')
                ->where('s_active', '=', 1)->orderBy('s_starttermin', 'desc')->take(50)->get();
    
    if (!empty(Input::get('date'))) {
        $latest = $latest->where('s_starttermin', '=', Input::get('date'))->orderBy('s_starttermin','desc')->get();
    }
    

    【讨论】:

      【解决方案3】:

      在您的逻辑中,一切都因您的 if 条件而中断。我建议您先在没有条件的情况下查询您的数据集,然后根据您的逻辑从结果集文件管理器中取出适当的记录。

      $all = Latest::select('s_customer', 's_product', 's_starttermin')
          ->leftJoin('temp_cat', 'temp_cat.spotid', '=', 'mc_spots.spotid')
          ->where('s_active', '=', 1)
          ->orderBy('s_starttermin', 'desc')
          ->limit(50)
          ->get(); 
      
      $latest = $all->first(); // because you use `desc` you get the latest here.
      
      if (!empty($date = Input::get('date'))) {
           // if the `$date` is preset and it's equal to `$data->s_starttermin`, you'll get it as the latest one.
           $latest = $all->filter(function($data) use ($date) {
                return $data->s_starttermin == $date;
           });
      }
      

      试一试,如果我在手机上的格式有误,请见谅:)

      【讨论】:

      • 我收到此错误.. Call to undefined method Illuminate\Database\Query\Builder::filter()
      • 糟糕,我调整了代码。我错过了get() 部分。请立即检查。
      • 但我仍然收到错误:Method limit does not exist.
      猜你喜欢
      • 2014-05-23
      • 2012-07-21
      • 2010-10-12
      • 2020-12-17
      • 2021-01-02
      • 1970-01-01
      • 2015-02-13
      • 2012-02-19
      相关资源
      最近更新 更多