Question: I'm using the following query to get records that exist between `start date` and `end date`:

```
$event =Event::whereBetween('date', [
        $request->start_date, $request->end_date
    ])->orderBy('date', 'desc')->paginate(5);

```

This is working when I provide the start date and end date but, when I provide only the start date, then I didn't get any record – but I want records that exist after the start date if the end date is kept blank.

Any suggestions?

Answer: 
Two alternatives from @babak-asharfi's answer

Using [`when()`](https://laravel.com/docs/8.x/queries#conditional-clauses)

```php

$event = Event::when($request->end_date, 
    function($query) use ($request) {
        $query->whereBetween('date', $request->start_date, $request->end_date);
    },
    function($query) use ($request){ 
        $query->where('date', '=>', $request->start_date);
    }
)->orderBy('date', 'desc')->paginate(5);

```

Or simply use a fallback date

```php
$event = Event::whereBetween('date', [
        $request->start_date, $request->end_date ?: '2999-12-31'
    ])->orderBy('date', 'desc')->paginate(5);

```
You can use below code

```
$query = Event::query();
            if ($request->has('start_date')){
               $query->whereBetween('date', [
                   $request->start_date, $request->end_date
               ]) 
            }else{
                $query->where('date', '>=', $request->start_date) 
            }
            $query->orderBy('date', 'desc')->paginate(5);

```