0

I am using laravel 5.3 and I have a scenario as follows

I have 2 models

  1. Robot
  2. Location

A Robot can have many Locations but a location can belong to only 1 Robot, and each time a Robot have a new location the count field in location Table will increment

what I am trying to is that I want to use polymorph relation with Laravel and Eloquent, I want to get all the Locations of all Robots by last updated by using distinct() ?

By the way, Locatable_id in location table is refers to Robot ID. here is my models

Robot Model

public function locations() {
      return $this->morphMany('App\Models\Location', 'locatable');
    }

Location Model

public function locatable() {
      return $this->morphTo();

}

Locations Table

enter image description here

Any help would be really appreciated. Thanks.

2 Answers2

1

Here is how I did solve it :)

// Grap all Robots with it's locations and Customer
  $robots = Robot::whereHas('locations', function($query){
    $query->whereNotNull('locatable_id')->orderBy('updated_at');
  })->with('locations')->with('customers')->get();

//Filter only latest Locations for each Robot
  $filterLocations= $robots->map(function($robot){
    $robot->timeEntries = $robot->locations()
      ->orderBy('updated_at', 'DESC')
      ->limit(1)
      ->get();
    return $robot;
  });

Appreciate your spending time for helping out :) Thank you samuele-colombo

Community
  • 1
  • 1
  • I'm glad to you. I think that your solution could be more elegant (maybe you could do it filtering the 'locations' directly in the first sentence). – Samuele Colombo Jan 18 '17 at 08:55
  • According with http://stackoverflow.com/questions/30231862/laravel-eloquent-has-with-wherehas-what-do-they-mean the `whereHas` returns only the `Robots` that have (_not null and ordered by updated_at_) `Locations`, but I think that you could directly filter them in the `with` method. – Samuele Colombo Jan 18 '17 at 09:09
0

The query below returns all the robots and for each one provides the latest location. The inner function could be used to filter the locations data.

Robot::with(['locations' => function($q) {
    $q->latest();
}])->get();


what I am trying to is that I want to use polymorph relation with Laravel and Eloquent, I want to get all the Locations of all Robots by last updated by using distinct() ?

This doesn't completely reach your goal but I think that is a good point to start.

Samuele Colombo
  • 685
  • 2
  • 6
  • 20
  • It is a one to many polomorphic relation since many objects might have locations for example, a user might have one ! since a Customer can have many robots and many users we can use "location" as an address keeper for customers for example. – Ahmed Abdulrahman Jan 17 '17 at 05:22
  • I'll edit my answer as soon as possibile. It shouldn't be tough achieve your goal. – Samuele Colombo Jan 17 '17 at 06:50
  • This is what I am trying using Tinker App\Models\Location::distinct('locateable_id')->get(['updated_at'])->all(); – Ahmed Abdulrahman Jan 17 '17 at 08:34
  • but it get even the old locations for the same robot!! I want only the latest one for each Robot :) – Ahmed Abdulrahman Jan 17 '17 at 08:35
  • Please check the new answer. It doesn't fit perfectly with your request, but honestly I'm a bit confused by your approch: It makes more sense (to me) starting from the robots and show theirs last location. – Samuele Colombo Jan 17 '17 at 19:53