简体   繁体   中英

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    |                |
+-------------+------------------+------+-----+---------+----------------+

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();
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();

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