如何从 Laravel 中的数据库 table 中检索记录并防止那些过期的记录
How to retrieve records from database table in Laravel and prevent those which are expired
所以我在 MySQL 中有一个 table 看起来像这样
这些基本上是由用户post编辑的广告。此 table 中的持续时间列是关于用户希望展示其广告的天数。这意味着如果用户选择 post 广告 3 天,我不应该只在 3 天后显示,这意味着它将在三天后过期。现在我只想检索那些未过期的记录。
我搜索了很多但找不到方法来做到这一点。请给我一个解决方案或参考 link 我可以找到解决方案的地方。
现在我正在使用这个脚本通过简单的分页获取记录
$items=Post::with('items','users')->paginate(12);
$categories = DB::table('item_categories')->get();
return view('search_post', compact('items','categories'));
你应该用你的分页做一个 Where 子句。
如此处所述:
喜欢:
$items=Post::with('items','users')
->where('created_at', '<=', Carbon::now()->addDays(3))
->paginate(12);
使用 Carbon 来做到这一点。
只需使用 Carbon addDay($duration)
方法将 duration
添加到 created_at
。
然后 return 你的帖子结果是 >= 到 today()
.
所以我把我的table改成了这个
并使用这个脚本
$items=Post::with('items','users')->where('duration','>=',\Carbon\Carbon::now() )->paginate(12);
$categories = DB::table('item_categories')->get();
return view('search_post', compact('items','categories'));
它运行良好。感谢大家。
所以我在 MySQL 中有一个 table 看起来像这样
这些基本上是由用户post编辑的广告。此 table 中的持续时间列是关于用户希望展示其广告的天数。这意味着如果用户选择 post 广告 3 天,我不应该只在 3 天后显示,这意味着它将在三天后过期。现在我只想检索那些未过期的记录。 我搜索了很多但找不到方法来做到这一点。请给我一个解决方案或参考 link 我可以找到解决方案的地方。
现在我正在使用这个脚本通过简单的分页获取记录
$items=Post::with('items','users')->paginate(12);
$categories = DB::table('item_categories')->get();
return view('search_post', compact('items','categories'));
你应该用你的分页做一个 Where 子句。
如此处所述:
喜欢:
$items=Post::with('items','users')
->where('created_at', '<=', Carbon::now()->addDays(3))
->paginate(12);
使用 Carbon 来做到这一点。
只需使用 Carbon addDay($duration)
方法将 duration
添加到 created_at
。
然后 return 你的帖子结果是 >= 到 today()
.
所以我把我的table改成了这个
并使用这个脚本
$items=Post::with('items','users')->where('duration','>=',\Carbon\Carbon::now() )->paginate(12);
$categories = DB::table('item_categories')->get();
return view('search_post', compact('items','categories'));
它运行良好。感谢大家。