首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带连接的Laravel orWhereRaw

带连接的Laravel orWhereRaw
EN

Stack Overflow用户
提问于 2022-08-09 22:06:50
回答 1查看 16关注 0票数 0

我正在努力使下面的查询正常工作。我最近遇到的错误是Object of class Closure could not be converted to string

在Laravel (5.2)的旧版本中,我读到:$queryJoinClause的实例,它没有whereRaw()方法。(https://stackoverflow.com/a/47672758/4113027)

如何编写此查询以使其工作?

代码语言:javascript
复制
$results = DB::table('statement_transactions AS st')
            ->join('quickbooks_transactions AS qt', function ($join) {
                $join->on('st.transaction_date', '=', 'qt.transaction_date');
                $join->on('st.transaction_type_id', '=', 'qt.transaction_type_id');
                $join->on('st.original_amount', '=', 'qt.original_amount');
                $join->on('st.balance', '=', 'qt.balance');
            })
            ->where('st.statement_id', $this->statement->id)
            ->where('st.has_match', 0)
            ->where('st.has_partial_match', 0)
            ->whereNull('st.quickbooks_transaction_id')
            ->orWhereRaw(function ($query) {
                $query->whereRaw("LEFT(REPLACE(st.invoice_nbr, '-', ''), 5) = LEFT(REPLACE(qt.invoice_nbr, '-', ''), 5)")
                    ->whereRaw("RIGHT(REPLACE(st.invoice_nbr, '-', ''), 5) = RIGHT(REPLACE(qt.invoice_nbr, '-', ''), 5)");
            })
            ->select('st.id AS statement_transaction_id', 'qt.id AS quickbooks_transaction_id')
            ->get();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-10 01:43:30

问题是:

代码语言:javascript
复制
->orWhereRaw(function ($query) {
    $query->whereRaw("LEFT(REPLACE(st.invoice_nbr, '-', ''), 5) = LEFT(REPLACE(qt.invoice_nbr, '-', ''), 5)")
    ->whereRaw("RIGHT(REPLACE(st.invoice_nbr, '-', ''), 5) = RIGHT(REPLACE(qt.invoice_nbr, '-', ''), 5)");
})

orWhereRaw()需要一个参数,一个字符串。相反,您将传递一个闭包(即您的function ($query) { ... })。

您需要将orWhereRaw更改为orWhere,后者确实接受闭包。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73298600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档