【问题标题】:SQL query to Eloquent conversionSQL 查询到 Eloquent 的转换
【发布时间】:2018-02-23 18:55:46
【问题描述】:

如何将下面的 SQL 语句转换为 Eloquent?

SELECT * FROM `messages` WHERE (`to`=$myID AND `from`=$guestID) OR (`to`=$guestID AND `from`=$myID)

【问题讨论】:

标签: php sql laravel eloquent


【解决方案1】:

parameter grouping 使用whereorWhere 闭包:

Message::where(function($q) use($myId, $guestId) {
    $q->where('to', $myId)->where('from', $guestId);
})
->orWhere(function($q) use($myId, $guestId) {
    $q->where('to', $guestId)->where('from', $myId);
})
->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    相关资源
    最近更新 更多