How to add multiple where clause on Eloquent ORM Laravel?

36,293

Solution 1

Just add one more where.

$testq= DB::table('attendances')
    ->where('user_id', '=', $userinput)
    ->where('logon', '=', $newdate)
    ->get();

http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Builder.html#method_where

$this where(string $column, string $operator = null, mixed $value = null, string $boolean = 'and')

Add a basic where clause to the query.

Solution 2

As an addition to @sectus's answer, you might like this syntax:

$testq= DB::table('attendances')->whereUserId($userinput)
                                ->whereLogon($newdate)
                                ->get();
Share:
36,293
jake balba
Author by

jake balba

Updated on December 02, 2020

Comments

  • jake balba
    jake balba over 3 years

    Having a hard time here trying to retrieve specific records where I need the ID and the Date to match, here is my code:

          $testq= DB::table('attendances')->where('user_id', '=', $userinput && 'logon', '=', $newdate)->get();
    
  • jake balba
    jake balba over 9 years
    excuse me for asking you one more, but can you tell me how to use if statement and determine if the record exist? 'if($testq exists)' ?
  • sectus
    sectus over 9 years
    @jakebalba, you could check $testq->count() laravel.com/api/4.2/Illuminate/Database/Eloquent/…
  • jake balba
    jake balba over 9 years
    so it will be like this ? 'if ($testq->count()<1)' sorry newbie here
  • sectus
    sectus over 9 years
    @jakebalba, you have wrote the code - do not ask - test it. Test takes less time than asking : )
  • sectus
    sectus over 9 years
    @jakebalba, add one more question with your code and attemps
  • jake balba
    jake balba over 9 years
    Still have another problem sir, I think the code is just using 'OR' and not 'AND'
  • sectus
    sectus over 9 years
  • Pathros
    Pathros about 8 years
    How about when you want to retrieve all the values (and including NULL) except one. In My case I don't get the NULL rows. For example, ->where('country_id','!=',120) But I don't get the NULL values. How do I fix this? -> I just found out by adding ->orWhereNull('country_id') ;)