draw134
draw134

Reputation: 1187

laravel query between two dates from two date columns database

I have a database events and it has 2 dates column named date_from which refers to start date and date_to refers to end date. The column event_date is when is the event posted. My question is how can I query this using the eloquent? I tried this code but it returns me an error. Type error: Argument 2 passed to Illuminate\Database\Query\Builder

Query

  $events = Event::where('school_id', '=', Auth::user()->school_id)
                      ->where('status', '=', 1)
                      ->where('active', '=', 1)
                      ->whereRaw("group_id in('$role', '0')")
                      ->whereBetween("date_from", "date_to" )
                      ->limit(2)
                      ->get();

Db structure

| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(191)     | NO   |     | NULL    |                |
| content     | text             | NO   |     | NULL    |                |
| group_id    | int(11)          | NO   |     | NULL    |                |
| event_date  | date             | YES  |     | NULL    |                |
| date_from   | date             | YES  |     | NULL    |                |
| date_to     | date             | YES  |     | NULL    |                |
| time_from   | time             | YES  |     | NULL    |                |
| time_to     | time             | YES  |     | NULL    |                |
| event_place | varchar(191)     | NO   |     | NULL    |                |
| image       | varchar(191)     | NO   |     |         |                |
| school_id   | int(11)          | NO   |     | NULL    |                |
| smsnotify   | int(11)          | NO   |     | 0       |                |
| appnotify   | int(11)          | NO   |     | 0       |                |
| status      | int(11)          | NO   |     | 0       |                |
| active      | int(11)          | NO   |     | 0       |                |
| updated_by  | varchar(191)     | YES  |     | NULL    |                |
| created_at  | timestamp        | YES  |     | NULL    |                |
| updated_at  | timestamp        | YES  |     | NULL    |                |
| users_id    | int(11)          | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

Upvotes: 0

Views: 5940

Answers (3)

Kishor Ahir
Kishor Ahir

Reputation: 1

  • I have tried this and it's working for me a two-column date range with two between dates

      | # | name    | from_date  | to_date    |
      | ----------- | ---------  | ---------- |-
      | 1 | Kishor  | 2021-05-17 | 2021-05-25 |
      | 2 | Martin  | 2021-05-24 | 2021-05-24 |
    
    
      select * from TableName  (`date_to` >= $from_date and `date_from` <= $to_date) 
    
      $query->where(function($query) use ($from, $to){
             $query->where('date_to', '>=', $from)
                ->where('date_from', '<=', $to);
             });
    

Upvotes: 3

Rahul Gupta
Rahul Gupta

Reputation: 1041

Use whereColumn of laravel query builder like this.

$events = Event::where('school_id', '=', Auth::user()->school_id)
                      ->where('status', 1)
                      ->where('active', 1)
                      ->whereRaw("group_id in('$role', '0')")
                       ->whereColumn([ ['date_from', '>=', 'event_date'], ['date_to', '<=', 'event_date'] ]);
                      ->limit(2)
                      ->get();

Upvotes: 1

draw134
draw134

Reputation: 1187

I just solved it by using two whereRaw statements

$now = Carbon::now()->toDateString();

$events = Event::where('school_id', '=', Auth::user()->school_id)
                      ->where('status', '=', 1)
                      ->where('active', '=', 1)
                      ->whereRaw("group_id in('$role', '0')")
                      ->whereRaw("date_from <=  date('$now')")
                      ->whereRaw("date_to >=  date('$now')")
                      ->limit(2)
                      ->get();

Upvotes: 1

Related Questions