繁体   English   中英

Laravel-Mongo数据库

[英]Laravel-mongo DB

我正在使用https://github.com/jenssegers/laravel-mongodb

    $query = $this->proposal->where('status', 'approved')
                ->with(['evaluations', 'user', 'contributions'])
                ->with(['reviews' => function ($q) {
                    $q->where('status','approved')->orderBy('created_at', 'desc');
                }]);

if( .... ) {

    dd($query->toSql()); // "select * from "proposals" where "status" = ?"

    $proposalsFoundByTile = $query->where('title','like',"%$request->search%")->get();

    dd($query->toSql()); // "select  from "proposals" where "status" = ? and "title" like ?"

    ...
}

为什么$ query值改变了? 如何“清除”此更改为初始值?

在这行之后

$proposalsFoundByTile = $query->where('title','like',"%$request->search%")->get();

放这个

$key = array_search("title", array_column($query->getQuery()->wheres, 
        'column'));
unset($query->getQuery()->wheres[$key]);

然后这将返回,

dd($query->toSql()); // "select  from "proposals" where "status" = ?"

正如注释中所建议的那样,使用clone()函数解决了此问题。

$query = $this->proposal->where('status', 'approved')
                ->with(['evaluations', 'user', 'contributions'])
                ->with(['reviews' => function ($q) {
                    $q->where('status','approved')->orderBy('created_at', 'desc');
                }]);

if( .... ) {

    $subQuery = clone($query);
    $proposalsFoundByTile = $subQuery->where('title','like',"%$request->search%")->get();

    ...

   $subQuery = clone($query);
   $some_var = $subQuery->... ; 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM