从日期中过滤日期,例如天,年,月

时间:2020-06-16 07:45:00

标签: php laravel

我有一个从以下代码中得到的数组:

$genreTag = Tag::with('tracks','elements')->where('category_id', $genresCat->id)->whereIn('id', $id)->get()->toArray();
$moodTag = Tag::with('tracks','elements')->where('category_id', $moodCat->id)->whereIn('id', $id)->get()->toArray();
$productionTag = Tag::with('tracks','elements')->where('category_id', $productionCat->id)->whereIn('id', $id)->get()->toArray();
$vocalTag = Tag::with('tracks','elements')->where('category_id', $vocalCat->id)->whereIn('id', $id)->get()->toArray();

$tag = array_merge($genreTag, $moodTag, $productionTag, $vocalTag);

当我点击“去年过滤器”复选框时,如何获取去年到今天的数据?

1 个答案:

答案 0 :(得分:0)

您可以使用whereBetween。您也不需要单独获取每个组然后合并它们,有一种名为whereIn的方法可以使用toArray()方法将您的集合转换为数组。让我与您分享一个简短的摘要。

$tags = Tag::with('tracks', 'elements')
->whereIn('category_id', [$genresCat->id, $moodCat->id, $productionCat->id, $vocalCat->id])
->whereBetween('created_at', [now()->toDateTimeString(), now()->subYear()->toDateTimeString()])
->get()
->toArray();