pm.calabrese
pm.calabrese

Reputation: 376

Issue with eager loading of neasted models with Laravel

Any idea why the following code is working proprely, returns the device with sensors and measuraments

public function getLastMinutes($device_id,$sensor_id,$minutes = 10) {
...
        $devices = Auth::user()->devices()->where("device_id",$device_id)->with(array('sensors.measurements' => function($query) use ($minutes,$sensor_id) {
            $date = new DateTime;
            $date->modify(-1*$minutes.' minutes');
            $formatted_date = $date->format('Y-m-d H:i:s');

            $query->where('measurements.created_at','>=',$formatted_date);

        }))->get()->toArray();
        return Response::json($devices);
...

but when i add the second where("sensor_id",$sensor_id) the measuraments disappiers from the json object returned

public function getLastMinutes($device_id,$sensor_id,$minutes = 10) {
...
        $devices = Auth::user()->devices()->where("device_id",$device_id)->with(array('sensors.measurements' => function($query) use ($minutes,$sensor_id) {
            $date = new DateTime;
            $date->modify(-1*$minutes.' minutes');
            $formatted_date = $date->format('Y-m-d H:i:s');

            $query->where("sensor_id",$sensor_id)->where('measurements.created_at','>=',$formatted_date);

        }))->get()->toArray();
        return Response::json($devices);
...

There is something i'm missing?

Thank you for your help!

Upvotes: 0

Views: 81

Answers (1)

pm.calabrese
pm.calabrese

Reputation: 376

I figure out the problem. by doing with(array('sensors.measurements .... in the object query you do not have any access to sensor... this means that by doing this $query->where("sensor_id",$sensor_id) the sensors_id will be searched on the measurements table... which is uncorrected. So the solution is have to with one that contains 'sensors' and another one that contains 'sensors.measuraments'.

So from this (wrong)

    public function getLastMinutes($device_id,$sensor_id,$minutes = 10) {
...
        $devices = Auth::user()->devices()->where("device_id",$device_id)->with(array('sensors.measurements' => function($query) use ($minutes,$sensor_id) {
            $date = new DateTime;
            $date->modify(-1*$minutes.' minutes');
            $formatted_date = $date->format('Y-m-d H:i:s');

            $query->where("sensor_id",$sensor_id)->where('measurements.created_at','>=',$formatted_date);

        }))->get()->toArray();
        return Response::json($devices);
...

to this one (correct)

...
$devices = Auth::user()->devices()->where("device_id",$device_id)
            ->with(array('sensors' => function($query) use ($sensor_id) {
                $query->where('sensors.sensor_id',$sensor_id);
            }))
            ->with(array('sensors.measurements' => function($query) use ($minutes,$sensor_id) {
                $date = new DateTime;
                $date->modify(-1*$minutes.' minutes');
                $formatted_date = $date->format('Y-m-d H:i:s');
                $query->where('measurements.created_at','>=',$formatted_date);
            }))
            ->get()->toArray();
            return Response::json($devices);
...

little tip. laravel-debugbar is a great project and you should check it out, but it actually did not help me much since i have a SPA and Laravel is doing a RESTFUL json server and for some reason laravel-debugbar did not kick in... but this little piece of code was atually great (taken from Get the query executed in Laravel 3/4)

        $queries = DB::getQueryLog();
        $last_query = end($queries);
        return Response::json($last_query);

Hope can help you

Upvotes: 1

Related Questions