Laravel-mongo DB

时间:2018-05-21 07:56:40

标签: mongodb laravel laravel-query-builder

我正在使用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 ?"

    ...
}

为什么$查询值已更改?我怎样才能清除"这会改变初始值吗?

2 个答案:

答案 0 :(得分:0)

在这一行之后,

$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" = ?"

答案 1 :(得分:0)

正如评论中所建议的,用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->... ; 
}