Use Or and AND condition at same time

Use Or and AND condition at same time

orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2
edited June 2017 in Free community support

Hello to All.

I need to use "OR" condition and "AND" condition at same time, but working only that one which is upper. Here is my code, can anyone tell me my error?

    ->where( function ( $q )  use( $todaydate1, $todaydate, $time, $paa ){
                //$q->where('username', $p)

                $q->where( 'date', $todaydate1, '=' );

                $q->or_where( function ( $r ) use( $todaydate, $time ) {
                $r->where( 'date', $todaydate );
                $r->where( 'time', $time, '>=' );
            } );
    } )

    ->where( 'username', $paa)

How to use them at same time?

Answers

  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin

    You need to perform grouping on the conditions: https://editor.datatables.net/docs/1.6.3/php/class-DataTables.Database.Query.html#_where_group .

    Click the row in the table for the where_group method to see an example of it being used.

    Allan

  • orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2

    Dear @allan,

    Can you write me an example where used "AND condition" with "OR condition" in "WHERE_GROUP". I could not exactly understand what I need to use

  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    ->where( function ( $q ) {
      $q->where( 'position', 'Manager' );
      $q->where_group( function ($q) {
          $q->where( 'location', 'Edinburgh' );
          $q->or_where( 'location', 'New York' );
        } );
    } )
    

    That will result in:

    position = Manager AND (location = Edinburgh OR location = New York
    

    Allan

This discussion has been closed.