【问题标题】:Laravel 4.2 - Eloquent WHERE clause with OR and reverse parametersLaravel 4.2 - 带有 OR 和反向参数的雄辩 WHERE 子句
【发布时间】:2014-12-02 09:18:03
【问题描述】:

我在 Laravel 4.2 中有这样的 Eloquent 语句

    $user_message_block = Message::where('responder_id', '=', Auth::user()->id)
                        ->where('user_id', '=', $user->id)->first();

如果$user_message_block 不存在,我还必须检查反向情况,我会这样做......

    if(!$user_message_block){
        $user_message_block = Message::where('responder_id', '=', Auth::user()->id)
                            ->where('user_id', '=', $user->id)->first();
    }

我真正想做的是运行一个查询来同时检查这两种情况..

在伪表达式中我需要这样的东西:

$user_message_block = Message::where('responder_id', '=', Auth::user()->id, 
                                 'AND', 'user_id', '=', $user->id, 
                                 'OR', 'responder_id', '=', $user->id, 
                                 'AND', 'user_id', '=', Auth::user()->id)->first();

所以基本上我需要

SELECT Message where (responder_id=x AND user_id=y) OR where (responder_id=y AND user_id=x)

我如何使用 Eloquent 做到这一点。我找不到更多关于 Eloquent 使用的 OR 和 AND 语句的信息。

谢谢!

更新:

经过更多查看,我发现这似乎有效(仍在测试很多)

$user_message_block = 
        Message::where(['responder_id' => Auth::user()->id, 'user_id' => $user->id])
             ->orWhere(['user_id' => Auth::user()->id, 'responder_id' => $user->id])
             ->first();

$user_message_block = 
        Message::where(['responder_id' => Auth::user()->id, 'user_id' => $user->id])
             ->orWhere(['user_id' => Auth::user()->id, 'responder_id' => $user->id])
             ->get();

我需要考虑这方面的任何缺点吗?

【问题讨论】:

    标签: php laravel-4 eloquent


    【解决方案1】:

    我将解决方案归功于 Noah,因为他提供了一个可行的解决方案以及他为帮助我做出的努力。然而,我确实最终使用了不同的解决方案..

    $user_message_block = 
        Message::where(['responder_id' => 'x', 'user_id'      => 'y'])
               ->orWhere(['user_id'    => 'x', 'responder_id' => 'y'])
               ->get();
    

    在我的特定情况下,我只需要找到的第一条记录,所以这对我有用

    $user_message_block = 
        Message::where(['responder_id' => 'x', 'user_id'      => 'y'])
               ->orWhere(['user_id'    => 'x', 'responder_id' => 'y'])
               ->first();
    

    我只想说,我在 Alex 的 phpacademy 视频中找到了一个类似于我的解决方案。我也偶然发现了这个Laravel eloquent SQL query with OR and AND with where clause

    【讨论】:

      【解决方案2】:

      在 Eloquent 中执行此操作可能非常复杂,但这正是您使用 Laravel 的查询构建器所需要的,而无需还原为原始 SQL。

          DB::table('messages')
              ->where(function($query)
              {
                  $query->where('responder_id', '=', 'x')
                  ->where('user_id', '=', 'y');
              })
              ->orWhere(function($query) {
                  $query->where('responder_id', '=', 'y')
                        ->where('user_id', '=', 'x');
              })
              ->get();
      

      这是我在上面运行 toSql() 得到的结果:

      select * from `messages` where (`responder_id` = ? and `user_id` = ?) or (`responder_id` = ? and `user_id` = ?)
      

      试试这个,看看它是否适用于 Eloquent。

      Message::where(function($query) {
          $query->where('responder_id', '=', 'x')
                ->where('user_id', '=', 'y');
      })->orWhere(function($query) {
          $query->where('responder_id', '=', 'y')
                ->where('user_id', '=', 'x');
      })->get();
      

      它应该可以工作。

      【讨论】:

      • mmmm,我刚刚发现了一些关于 Eloquent 的东西似乎有效,我会更新我的问题。看起来我可以传递信息数组并使用 ->orWhere 雄辩的子句。
      • 如果这为您提供了解决方案,请接受它作为答案。
      • 我正在使用我在上面发布的解决方案。稍后我将对此进行测试,看看是否有任何明显的差异使该解决方案更可行。
      • 只获取第一条记录。
      • 我只需要第一条记录。您发布的解决方案引发错误,
      猜你喜欢
      • 2014-08-23
      • 2021-02-13
      • 2016-07-05
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      相关资源
      最近更新 更多