0
SELECT * FROM mytable WHERE created_date 
    BETWEEN CURDATE() - INTERVAL 90 DAY AND CURDATE();

How can I write this query in Laravel?

3 Answers 3

2

Laravel have a good solution for this and it is whereBetween see that here https://laravel.com/docs/5.6/queries

Sign up to request clarification or add additional context in comments.

Comments

1

I think this will solve your problem:

use Carbon\Carbon;

$now = Carbon::now();
$prev = Carbon::now()->subDays(90);

DB::table('mytable')->where(function ($query) use ($now,$prev) {
    $query->whereBetween('created_date', [$prev, $now]);
})->get();

Comments

0

The following query gives you records that has date between now and date 90 days back:

$now = date('Y-m-d'); // now
$old = date('Y-m-d', strtotime('-90 days')); // 90 days back
$records = DB::table('mytable')->where('created_date', '>=', $old)
                       ->where('created_date', '<=', $now)
                       ->get();

You should read Laravel Docs: https://laravel.com/docs/5.1/queries#where-clauses

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.