我试图创建一个报告页面,显示从特定日期到特定日期的报告。这是我当前的代码:

$now = date('Y-m-d');
$reservations = Reservation::where('reservation_from', $now)->get();

这在普通SQL中所做的是从表中选择*,其中reservation_from = $now。

我在这里有这个问题,但我不知道如何将其转换为雄辩的问题。

SELECT * FROM table WHERE reservation_from BETWEEN '$from' AND '$to

如何将上面的代码转换为雄辩的查询?


当前回答

我遵循了其他贡献者提供的有价值的解决方案,遇到了一个没有人解决的小问题。如果reservation_from是一个datetime列,那么它可能不会产生预期的结果,并且会错过所有日期相同但时间高于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();

其他回答

试试这个:

因为你是基于单列值进行抓取,你可以同样简化你的查询:

$reservations = Reservation::whereBetween('reservation_from', array($from, $to))->get();

根据条件检索:laravel文档

希望这对你有所帮助。

我已经创建了模型作用域

要了解更多关于范围的信息,请查看以下链接:

laravel.com/docs/eloquent # query-scopes medium.com/@janaksan / using-scope-with-laravel

代码:

   /**
     * 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);
    }

在控制器中,将Carbon Library添加到顶部

use Carbon\Carbon;

从现在开始获取最近十天的记录

 $lastTenDaysRecord = ModelName::whereDateBetween('created_at',(new Carbon)->subDays(10)->startOfDay()->toDateString(),(new Carbon)->now()->endOfDay()->toDateString() )->get();

从现在开始获取过去30天的记录

 $lastThirtyDaysRecord = ModelName::whereDateBetween('created_at',(new Carbon)->subDays(30)->startOfDay()->toDateString(),(new Carbon)->now()->endOfDay()->toDateString() )->get();

@masoud,在Laravel,你必须从表单请求字段值。所以,

    Reservation::whereBetween('reservation_from',[$request->from,$request->to])->get();

而在livewire中,略有变化

    Reservation::whereBetween('reservation_from',[$this->from,$this->to])->get();

让我用准确的时间戳添加正确的语法

之前

$from = $request->from;
$to = $request->to;
Reservation::whereBetween('reservation_from', [$from, $to])->get();

$from = date('Y-m-d', strtotime($request->from));
$to = date('Y-m-d', strtotime($request->to));
Reservation::whereBetween('reservation_from', [$from, $to])->get();

注意:如果您以字符串形式存储日期,那么请确保在from和to中传递准确的格式。它将与DB匹配。

我遵循了其他贡献者提供的有价值的解决方案,遇到了一个没有人解决的小问题。如果reservation_from是一个datetime列,那么它可能不会产生预期的结果,并且会错过所有日期相同但时间高于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();