Laravel 버전 11.42는 날짜 관련 쿼리 로직을 단순화하는 실용적인 상대 날짜 쿼리 빌더 메소드 세트를 소개합니다. 이러한 방법을 사용하기 위해 모든 응용 프로그램을 리팩터링 할 필요는 없지만 모델의 상대 날짜 논리에보다 간결하고 읽기 쉬운 이점을 제공합니다.
Article
날짜는 현재 시간과 동일해야합니다.
published_at
use Illuminate\Database\Eloquent\Builder; use App\Models\Article; public function scopeActive(): Article|Builder { return $this->where('status', ArticleStatus::Published) ->where('published_at', '<=', now()); }와 일치 할 수 있습니다
Article::with('user', 'category', 'tags') ->active() ->orderByDesc('published_at') ->limit(20) ->get();
및 scopeActive()
변형이 포함됩니다.
whereNowOrPast
Laravel V11.42에 추가 된 모든 새로운 상대 날짜 방법에 대해서는 풀 요청 #54408을 확인하십시오. 이 방법은
$this->where('status', ArticleStatus::Published) ->whereNowOrPast('published_at');
위 내용은 Laravel의 쿼리 빌더에서 상대 날짜 도우미를 사용합니다의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!