代码之家  ›  专栏  ›  技术社区  ›  Jessey Fransen

在Laravel中检索更新的\u at比创建的\u at早2小时的记录(雄辩)

  •  3
  • Jessey Fransen  · 技术社区  · 7 年前

    我想统计更新时间比创建时间早2小时的记录。

    密码

    $teLang = $kentekens->where('updated_at', '>', 'created_at'->subHours(2));
    

    不幸的是,这不起作用,因为次小时(2)表示碳,但希望你能理解。

    看法

    <h1>{{ $teLang->count() }}</h1>
    

    控制器

    public function create() {
            $kentekens = Kenteken::latest()
                ->where('created_at', '>=', Carbon::today())
                ->get();
    
            $teLang = $kentekens->where('updated_at', '>', 'created_at'->subHours(2));
    
            return view('layouts.dashboard', compact('kentekens', 'teLang'));
        }
    

    有人知道怎么做吗?

    3 回复  |  直到 7 年前
        1
  •  3
  •   odan    7 年前
    $kentekens->whereRaw('`updated_at` > DATE_ADD(`created_at`, INTERVAL 2 HOUR)');
    

    SQL:

    select `id` from `tablename` where `updated_at` > DATE_ADD(`created_at`, INTERVAL 2 HOUR)
    
        2
  •  1
  •   Alexey Mezenin    7 年前

    使用 filter() 方法使用您的代码逻辑:

    $teLang = $kentekens->filter(function($i) {
        return $i->updated_at->gt($i->created_at->subHours(2));
    });
    

    使用以下语句“where updated\u at比created\u at早2小时”:

    $teLang = $kentekens->filter(function($i) {
        return $i->updated_at->lt($i->created_at->subHours(2));
    });
    
        3
  •  0
  •   Shtirlic Otto    7 年前

    也许是吧?

    public function create() {
        $kentekens = Kenteken::latest()
            ->where('created_at', '>=', Carbon::today())
            ->get();
        $cr = Carbon::create($kentekens->created_at);
        $teLang = $kentekens->where('updated_at', '>', $cr->addHours(2));
    
        return view('layouts.dashboard', compact('kentekens', 'teLang'));
    }