0

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

3 Answers 3

3
  • 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);
             });
    
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

1 Comment

something similar with mine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.