Php 拉威尔+;数据库中的Mongo DB最新唯一项

Php 拉威尔+;数据库中的Mongo DB最新唯一项,php,laravel,mongodb,jenssegers-mongodb,Php,Laravel,Mongodb,Jenssegers Mongodb,我正在从事一个Laravel+Mongo DB项目,在该项目中,我们存储的数据如下: _id | machinename | SoftwareKey | created_at ----------------------------------------- 5cf6c4a7fdce2778e833dd88 | VM007 | TestSoftware | 2017-01-30 13:22:39 ----------------------

我正在从事一个Laravel+Mongo DB项目,在该项目中,我们存储的数据如下:

_id                      | machinename | SoftwareKey  | created_at
-----------------------------------------
5cf6c4a7fdce2778e833dd88 | VM007       | TestSoftware | 2017-01-30 13:22:39
-----------------------------------------
5cf6c4a7fdce2778e833324  | VM008       | TestSoftware | 2017-01-30 13:23:42
-----------------------------------------
5cf6c4a7fdce27783244355  | VM009       | TestSoftware | 2017-01-30 13:25:54
-----------------------------------------
5cf6c4a7fdce27723424345  | VM007       | TestSoftware | 2017-01-30 13:25:
-----------------------------------------
5cf6c4a7fdce23432432745  | VM008       | TestSoftware | 2017-01-30 13:25:54
-----------------------------------------
5cf6c4a7fdce27732467876  | VM009       | TestSoftware | 2017-01-30 13:25:33
我需要调整从该表返回所有条目的方法,以仅返回机器名称的最新条目,基本上返回此部分:

_id                      | machinename | SoftwareKey  | created_at
-----------------------------------------
5cf6c4a7fdce2778e833dd88 | VM007       | TestSoftware | 2017-01-30 13:22:39
-----------------------------------------
5cf6c4a7fdce2778e833324  | VM008       | TestSoftware | 2017-01-30 13:23:42
-----------------------------------------
5cf6c4a7fdce2778324      | VM009       | TestSoftware | 2017-01-30 13:25:54
指数法:

public function index() {
    $hills = Hill::all();

    return fractal()
        ->collection($hills)
        ->transformWith(new HillTransformer)
        ->toArray();
    }
我发现了这个问题,并尝试调整我的方法:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Hill;
use App\Transformers\HillTransformer;
use DB;

class HillController extends Controller
{
    public function index() {
       $hills = DB::select("SELECT *
       FROM hills
       JOIN (SELECT machinename, MAX(created_at) created_at
               FROM hills
           GROUP BY machinename) machinenames
         ON machinename = machinenames.machinename
        AND created_at = machinenames.created_at");

        return fractal()
            ->collection($hills)
            ->transformWith(new HillTransformer)
            ->toArray();
    }
}
但我有一个例外:

“消息”:“对空成员函数prepare()的调用”, “异常”:“Symfony\Component\Debug\exception\FatalThrowableError”, “文件”:“/home/vagrant/code/hilltracking/vendor/laravel/framework/src/light/Database/Connection.php”, “行”:326

有没有其他方法可以做到这一点?谢谢

顺便说一句,我正在使用和分形库