Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - select row between start date and end date using eloquent

I want to convert this query into laravel eloquent,

select * from schedule where (now() between start_date and end_date);

I tried using whereBetween, but I got some error.

$schedule = Schedule::whereBetween(Carbon::now(), ['start_date', 'end_date'])->get();

the error looks like this

QueryException in Connection.php line 647: SQLSTATE[42S22]: Column not found: 1054 Unknown column '2017-06-01 06:17:30' in 'where clause' (SQL: select * from schedule where 2017-06-01 06:17:30 between start_date and end_date)

any idea?

like image 902
Ahmad Jamil Al Rasyid Avatar asked Jun 01 '17 06:06

Ahmad Jamil Al Rasyid


People also ask

How can we get data between two dates using query in Laravel?

Try to do something like this: $date1 = Carbon::today()->toDateString(); $date2 = Carbon::today()->toDateString(); $myModel = MyModel::find(1); $myModel->whereBetween('created_at', [$date1, $date2]); $myModel->get();

How do I use whereBetween in Laravel?

The whereBetween() method is a query builder chained alongside other Laravel query builders used to fetch data from the database. The whereBetween() method queries the database table to fetch rows of records from the database within a range of values.

How can I get data between two dates in SQL?

SELECT * FROM ATM WHERE TRANSACTION_TIME BETWEEN '2005-02-28 21:00:00' AND '2008-12-25 00:00:00';

What is the difference between eloquent and query builder in Laravel?

Eloquent ORM is best suited working with fewer data in a particular table. On the other side, query builder takes less time to handle numerous data whether in one or more tables faster than Eloquent ORM. In my case, I use ELoquent ORM in an application with tables that will hold less than 17500 entries.


1 Answers

$from = $request->from;
$to = $request->to;
$title="Sales From: ".$from." To: ".$to;
$sales = Sale::whereBetween('created_at', [$from.' 00:00:00',$to.' 23:59:59'])->get();
like image 56
THEMBO CHARLES LWANGA Avatar answered Nov 04 '22 03:11

THEMBO CHARLES LWANGA