2 months ago

Where query column if column has value

Posted 2 months ago by Dosmukhanbet

Hi everyone, please help to solve below issue There is table with column date_from and date_to. date_to column is optional, so it might be null. I want to make query this column if it has value, if it is null no need to run query at all. Is there way to make query conditionally depending column value? I have tries as below but it does not work as expected.

trips()->with('user', 'a_point', 'b_point')
                                    ->where('date_from', '>=' , Carbon::today())
                                    ->where('date_from', '>=' , Carbon::parse($date)->format('Y-m-d'))
                                    ->where( function ( $query ) use ( $date ) {
                                        $query->whereNotNull( 'date_to' )
                                              ->orWhere('date_to', '<=' , Carbon::parse($date)->format('Y-m-d'));

Please sign in or create an account to participate in this conversation.