Alex
Alex

Reputation: 2775

WhereBetween does not work correctly in Laravel

Records in MySQL database looking like 06/30/1986. Using format "m/d/Y".

I'm trying to get the data using the function:

$users = DB::table('users')
            ->whereBetween('date', [07/04/1960, 07/04/2016])
            ->get();

But records from DB are not displayed with this query.

Upvotes: 2

Views: 504

Answers (2)

Alex
Alex

Reputation: 2775

Column type in MySQL DB changed from VARCHAR to DATE, changed format to "Y-m-d", and its working!

Upvotes: 1

Dry7
Dry7

Reputation: 881

Try

use Carbon\Carbon;

$first = Carbon::createFromDate(1960,4,7);
$second = Carbon::createFromDate(2016,4,7);

$users = DB::table('users')
            ->whereBetween('date', [$first->toDateTimeString(), $second->toDateTimeString()])
            ->get();

Upvotes: 1

Related Questions