Php 从数据库中获取最新数据
我需要从表中获取最新数据。我在URL中传递了日期,并使用它进行筛选。所以,我有这样的想法:Php 从数据库中获取最新数据,php,laravel,Php,Laravel,我需要从表中获取最新数据。我在URL中传递了日期,并使用它进行筛选。所以,我有这样的想法: $latest = Latest::select('s_customer', 's_product', 's_starttermin') ->leftJoin('temp_cat', 'temp_cat.spotid', '=', 'mc_spots.spotid') ->where('s_active', '=', 1) ->orderB
$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\u starttermin
..进行过滤,我创建了一个函数来调用它,直到我们找到最新的记录,但请小心,如果没有任何记录,它将循环到无穷大,我会添加一些$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')
我创建了一个函数来调用它,直到我们找到了最新的记录,但请注意,如果没有任何记录,它将循环到无穷大,我将添加一些
$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')
在您的逻辑中,一切都会随着您的
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;
});
}
试一试,如果格式错误,请道歉,就像我在手机上说的那样:)在你的逻辑中,如果你的
条件,一切都会中断。我建议您先在没有条件的情况下查询数据集,然后根据您的逻辑从结果集文件管理器中输出适当的记录
$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;
});
}
试一试,如果格式错误,请向我道歉,因为我在我的手机上:)这可能会对你有所帮助
$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();
}
也许这对你有帮助
$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();
}
但我必须得到指定日期的结果。这就是为什么我的表单中有筛选日期,它作为Input::get('date')
传递。。如果今天没有结果,我需要显示昨天的最新结果(例如,您误解了我)。这将显示少于今天日期的所有结果。我只想在指定日期显示。如果2017-09-28的2017-09-28
没有结果,而最后一个结果来自2017-09-27
,那么我想显示2017-09-27的所有结果,但我必须获得指定日期的结果。这就是为什么我的表单中有筛选日期,它作为Input::get('date')
传递。。如果今天没有结果,我需要显示昨天的最新结果(例如,您误解了我)。这将显示少于今天日期的所有结果。我只想在指定日期显示。如果2017-09-28
没有结果,而最后一个结果来自2017-09-27
,那么我想显示2017-09-27
的所有结果,我得到了这个错误<代码>调用undefined方法Illumb\Database\Query\Builder::filter()
Oops,我调整了代码。我错过了get()
部分。请现在检查。但我仍然得到错误:方法限制不存在。
我得到此错误<代码>调用undefined方法Illumb\Database\Query\Builder::filter()Oops,我调整了代码。我错过了get()
部分。请现在检查。但我仍然得到错误:方法限制不存在。