4

I'm stuck at the moment and hope someone can give me a hand. I'm using a polymorphic relation and want to search my database for rows that fulfill conditions in the "parent" and the "child" table.

To get concrete, one small example. Given the following structure I e.g. want to look for a property with price "600" and rooms "3". Is there a way to do that with eloquent?



Tables

Table properties (parent)

  • id
  • price
  • details_type [can be "Apartment" or "Parcel"]
  • details_id

Table apartments (child)

  • id
  • rooms

Table parcels (child)

  • id
  • ... (does not have a "rooms" column)



Relationships

Class Property

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

Classes Apartment + Parcel

public function property() {
  return $this->morphMany('Property', 'details')
}




What I tried

A lot, really. But somehow I'm always doing something wrong or missing something. The solutions that, in my opinion should work are either:

Property::with(array('details' => function($query) {
                  $query->where('rooms', 3);
             }));

or

Property::with('details')
        ->whereHas('details', function($query) {
            $query->where('rooms', '=', '3');
        });

But in both cases I get the following FatalError.:

Class name must be a valid object or a string



Has anyone of you already had a similar problem? Thank you very much for any kind of hint.

Community
  • 1
  • 1
MrSnoozles
  • 860
  • 8
  • 13

1 Answers1

5

Let's start with your naming convention:

public function detail() // It relates to a single object
{
    return $this->morphTo();
}

And

public function properties() // It relates to several objects
{
    return $this->morphMany('Property', 'details')
}

Then you would be able to do this:

$properties = Property::whereHas('details', function($q)
{
    $q->where('rooms', '=', '3');
})
->where('price', '=', 600)
->get();

Please note that this will never return a Parcel, since there isn't a parcel with a room.

Ronald Hulshof
  • 1,986
  • 16
  • 22
  • Thank you so much for your response. Your suggested solution is what I thought too. However doing that I get an error: `Symfony\Component\Debug\Exception\FatalErrorException Class name must be a valid object or a string`. It seems to me that `whereHas` does not support polymorphic relations yet. – MrSnoozles Feb 07 '14 at 09:35
  • Ah, I now realize what the problem is. Eloquent does not support Eager Loading of Polymorphic Childs as of yet. Eloquent is only able to Eager Load Polymorphic Parents. I would advise you to use the Query Builder instead and do it the raw way. :) – Ronald Hulshof Feb 14 '14 at 09:12