使用MongoDB和Laravel进行文本搜索

I'm using jenssegers/laravel-mongodb package for using MongoDB with Laravel:

And I'm trying to implement text search with MongoDB using this command:

db.place.find({$text:{$search:"hotel"}})
But I don't know how to translate this query to using it with above package and Laravel.

Well, how funny, I just found out the answer. Here is the code to get the result from text search:

\DB::collection('place')->raw()->find(array('$text'=>array('$search'=>'hotel')))->getNext();

You can use whereRaw method.

Place::whereRaw(['$text' => ['$search' => 'hotel']])->get();

You can even make use of laravel query scope for defining common sets of constraints that you may easily re-use throughout your application.

Place Model

public function scopeContains($query, $search)
{
    return $query->whereRaw(['$text' => ['$search' => $search]]);
}

Place Controller

Place::contains('hotel')->get();