wob*_*ano 89 php orm laravel laravel-5
我正在尝试创建一个报告页面,显示从特定日期到特定日期的报告.这是我目前的代码:
$now = date('Y-m-d');
$reservations = Reservation::where('reservation_from', $now)->get();
这在纯SQL中的作用是什么select * from table where reservation_from = $now.
我在这里有这个查询,但我不知道如何将其转换为雄辩的查询.
SELECT * FROM table WHERE reservation_from BETWEEN '$from' AND '$to
如何将上面的代码转换为雄辩的查询?先感谢您.
Pet*_*ota 173
该whereBetween方法验证列的值是否在两个值之间.
$from = date('2018-01-01');
$to = date('2018-05-02');
Reservation::whereBetween('reservation_from', [$from, $to])->get();
在某些情况下,您需要动态添加日期范围.根据@Anovative的评论你可以这样做:
Reservation::all()->filter(function($item) {
  if (Carbon::now->between($item->from, $item->to) {
    return $item;
  }
});
如果您想添加更多条件,那么您可以使用orWhereBetween.如果您想排除日期间隔,那么您可以使用whereNotBetween.
Reservation::whereBetween('reservation_from', [$from1, $to1])
  ->orWhereBetween('reservation_to', [$from2, $to2])
  ->whereNotBetween('reservation_to', [$from3, $to3])
  ->get();
其他有用的where子句:whereIn,whereNotIn,whereNull,whereNotNull,whereDate,whereMonth,whereDay,whereYear,whereTime,whereColumn,whereExists,whereRaw.
以下应该有效:
$now = date('Y-m-d');
$reservations = Reservation::where('reservation_from', '>=', $now)
                           ->where('reservation_from', '<=', $to)
                           ->get();
如果您的字段datetime不是date(尽管它适用于两种情况)的另一个选项:
$fromDate = "2016-10-01";
$toDate   = "2016-10-31";
$reservations = Reservation::whereRaw("reservation_from >= ? AND reservation_from <= ?", 
    array($fromDate." 00:00:00", $toDate." 23:59:59")
)->get();
如果要在db:=>中检查两个日期之间是否存在当前日期,则查询将获取应用程序列表,如果今天的员工应用程序在今天的日期存在.
$list=  (new LeaveApplication())
            ->whereDate('from','<=', $today)
            ->whereDate('to','>=', $today)
            ->get();
我已经创建了模型范围
有关范围的更多信息:
代码:
   /**
     * Scope a query to only include the last n days records
     *
     * @param  \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeWhereDateBetween($query,$fieldName,$fromDate,$todate)
    {
        return $query->whereDate($fieldName,'>=',$fromDate)->whereDate($fieldName,'<=',$todate);
    }
并在控制器中,将碳库添加到顶部
use Carbon\Carbon;
或者
use Illuminate\Support\Carbon;
从现在开始获取最近 10 天的记录
 $lastTenDaysRecord = ModelName::whereDateBetween('created_at',(new Carbon)->subDays(10)->startOfDay()->toDateString(),(new Carbon)->now()->endOfDay()->toDateString() )->get();
从现在开始获取最近 30 天的记录
 $lastTenDaysRecord = ModelName::whereDateBetween('created_at',(new Carbon)->subDays(30)->startOfDay()->toDateString(),(new Carbon)->now()->endOfDay()->toDateString() )->get();
试试这个:
由于您基于单个列值进行提取,因此您可以同样简化查询:
$reservations = Reservation::whereBetween('reservation_from', array($from, $to))->get();
根据条件检索:laravel docs
希望这有帮助.
小智 7
如果你需要在日期时间字段应该是这样的。
return $this->getModel()->whereBetween('created_at', [$dateStart." 00:00:00",$dateEnd." 23:59:59"])->get();
小智 6
我遵循了其他贡献者提供的有价值的解决方案,但遇到了一个没有人解决的小问题。如果reservation_from是日期时间列,则它可能不会按预期生成结果,并且会错过日期相同但时间大于 00:00:00 时间的所有记录。为了改进上面的代码,需要像这样进行一些小的调整。
$from = Carbon::parse();
$to = Carbon::parse();
$from = Carbon::parse('2018-01-01')->toDateTimeString();
//Include all the results that fall in $to date as well
$to = Carbon::parse('2018-05-02')
    ->addHours(23)
    ->addMinutes(59)
    ->addSeconds(59)
    ->toDateTimeString();
//Or $to can also be like so
$to = Carbon::parse('2018-05-02')
    ->addHours(24)
    ->toDateTimeString();
Reservation::whereBetween('reservation_from', [$from, $to])->get();
| 归档时间: | 
 | 
| 查看次数: | 137129 次 | 
| 最近记录: |