简体   繁体   中英

how to convert core query of sql in laravel?

i am using laravel 5.2 framework. I want to find the all events that happens in next seven days. Here i have successfully find the core query but i want to convert with laravel query Can anyone help me

Here is my query

SELECT * FROM `allocations` WHERE `date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY) ;

Can anyone help me. Thanks in advance :)

Try this:

Allocation::where('date', '>', Carbon::now())
          ->where('date', '<', Carbon::now()->addWeek())
          ->get();

update

I'm not sure if this will also work but please try;

Allocation::whereBetween('date', [Carbon::now(), Carbon::now()->addWeek()])->get();

Simple:

DB::select("SELECT * FROM `allocations` WHERE `date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)");

A little more complicated:

DB::table('allocations')->whereRaw("`date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)")->get();

If you have an Eloquent model set up, then replace DB::table()-> with your model name:

Allocations::whereRaw("`date` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)")->get();

Using Eloquent Models:

use App\Allocation; // Assuming this is your Eloquent model
use DB; 

Allocation::whereBetween('date', [DB::raw('NOW()'), 
    DB::raw('DATE_ADD(NOW(), INTERVAL 7 DAY)']);

Using DB Facade

use DB; 
DB::table('allocations')->whereBetween('date', [DB::raw('NOW()'), 
    DB::raw('DATE_ADD(NOW(), INTERVAL 7 DAY)']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM