Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lifesound's avatar

Get posts from date to date

How to get posts from to knowing that i have 2 datepickers form .

this is the request array :

      "_token" => ""
     "from" => "2016-07-05"
     "to" => "2016-07-28"
0 likes
10 replies
d3xt3r's avatar

Do you want us to write the query ??? Why don't you show us what you have already done and we shall take it from there ? First try with plain vanilla sql in some workbench and then try convert it to eloquent.

1 like
jlrdw's avatar

Write a regular query it's perfectly OK in laravel to do so.

d3xt3r's avatar

With modern frameworks, the beauty is, THINK LOUD of what you want, and the method/function name will be very close to what you would have thought :)

1 like
lifesound's avatar
        $posts = \DB::table('posts')->whereBetween('created_at',[strtotime('-1 month'),strtotime('today')])->get();

does not work

may be I'm stupid

d3xt3r's avatar

Use Carbon to create the dates $from and $to

bobbybouwmann's avatar
Level 88

The query is expecting some kind of format here. The best way is using the build in Carbon classes

$from = \Carbon\Carbon::today()->subMonth();
$to = \Carbon\Carbon::today();

$posts = Post::whereBetween('created_at', [$from, $to])->get();

Carbon: http://carbon.nesbot.com/docs/

1 like

Please or to participate in this conversation.