9

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();
Rishabh
  • 3,752
  • 4
  • 47
  • 74
jake balba
  • 413
  • 2
  • 7
  • 18

2 Answers2

8

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.

sectus
  • 15,605
  • 5
  • 55
  • 97
  • 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)' ? – jake balba Nov 25 '14 at 07:06
  • @jakebalba, you could check $testq->count() http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_count – sectus Nov 25 '14 at 07:17
  • so it will be like this ? 'if ($testq->count()<1)' sorry newbie here – jake balba Nov 25 '14 at 07:28
  • 1
    @jakebalba, you have wrote the code - do not ask - test it. Test takes less time than asking : ) – sectus Nov 25 '14 at 07:32
  • @jakebalba, add one more question with your code and attemps – sectus Nov 25 '14 at 07:39
  • Still have another problem sir, I think the code is just using 'OR' and not 'AND' – jake balba Nov 25 '14 at 07:46
  • @jakebalba, http://stackoverflow.com/questions/16995102/laravel-4-eloquent-where-with-or-and-or – sectus Nov 25 '14 at 08:31
  • 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')` ;) – Pathros Mar 03 '16 at 17:57
5

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

$testq= DB::table('attendances')->whereUserId($userinput)
                                ->whereLogon($newdate)
                                ->get();
TheBurgerShot
  • 1,586
  • 2
  • 11
  • 20