你如何更改为 laravel 语法?

how do you change to laravel syntax?

我有一个 SQL 查询,我想将其更改为 Laravel Eloquent 或查询生成器。

$myDate = date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "+1 month" ) );


$result = mysql_query( 'SELECT * FROM myTable WHERE expire_date BETWEEN NOW AND "' . $myDate . '"' );

请帮帮我!

你需要做这样的事情:

假设 myTable 有一个名为 MyTable 的模型:

$now = Carbon::now();
$nowPlus1Month = Carbon::now()->addMonth();
//If you want to sub a month:
$nowMinusMonth = Carbon::now()->subMonth();

$result = MyTable::whereBetween('expire_date', [$now, $nowPlus1Month])->get();

// Or
$result = MyTable::whereBetween('expire_date', [$nowMinusMonth, $now])->get();

希望对您有所帮助。

试试这个,

 $now = Carbon::now();
 $nowPlus1Month = Carbon::now()->addMonth();

 $result = MyTable::whereBetween('expire_date', [$now, $nowPlus1Month])->get();
 //Substract 1 month from the date
 $result = MyTable::whereBetween('expire_date', [$now, Carbon::now()->subDays(30)])->get();