【问题标题】:How to add multiple where clause on Eloquent ORM Laravel?如何在 Eloquent ORM Laravel 上添加多个 where 子句?
【发布时间】:2015-01-23 01:54:20
【问题描述】:

在这里尝试检索需要 ID 和日期匹配的特定记录时遇到了困难,这是我的代码:

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

【问题讨论】:

    标签: php oop laravel-4 orm eloquent


    【解决方案1】:

    在哪里再添加一个。

    $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, 混合 $value = 空,字符串 $boolean = 'and')

    在查询中添加一个基本的 where 子句。

    【讨论】:

    • 不好意思再问你一句,请问如何使用if语句判断记录是否存在? '如果($testq 存在)' ?
    • @jakebalba,你可以检查 $testq->count() laravel.com/api/4.2/Illuminate/Database/Eloquent/…
    • 所以会是这样吗? 'if ($testq->count()
    • @jakebalba,你已经写了代码——不要问——测试它。测试比询问花费的时间更少:)
    • @jakebalba,用你的代码和尝试再添加一个问题
    【解决方案2】:

    作为@sectus 答案的补充,您可能会喜欢这种语法:

    $testq= DB::table('attendances')->whereUserId($userinput)
                                    ->whereLogon($newdate)
                                    ->get();
    

    【讨论】:

      猜你喜欢
      • 2019-11-27
      • 2016-06-13
      • 2022-10-08
      • 2013-10-19
      • 2023-03-28
      • 1970-01-01
      • 2014-08-19
      相关资源
      最近更新 更多