iverberk / larasearch

Searchable Eloquent Models
MIT License
225 stars 48 forks source link

Polymorphic relationships #57

Open threesquared opened 9 years ago

threesquared commented 9 years ago

Is there support for polymorphic relationships? I see there is a check in PathsCommand for polymorphic relationships. However when I run the command on my model it doesn't get past line 159 as the related model is an instance of the ancestor.

I have

namespace App\Models;
use App\Models\Eloquent;
use Iverberk\Larasearch\Traits\SearchableTrait;
class QuoteRequest extends Eloquent
{
    use SearchableTrait;
    /**
     * @return \Illuminate\Database\Eloquent\Relations
     */
    public function details()
    {
        return $this->morphTo();
    }
}

and

namespace App\Models\QuoteRequest\Letting;
use App\Models\Eloquent;
use Iverberk\Larasearch\Traits\TransformableTrait;
use Iverberk\Larasearch\Traits\CallableTrait;
class Details extends Eloquent
{
    use TransformableTrait, CallableTrait;
    /**
     * @return \Illuminate\Database\Eloquent\Relations
     */
    public function quoterequest()
    {
        return $this->morphOne('App\Models\QuoteRequest', 'details');
    }

}

but the path command only returns

{
    "paths": {
        "App\\Models\\QuoteRequest": [

        ]
    },
    "reversedPaths": {
        "App\\Models\\QuoteRequest": [
            ""
        ]
    }
}
iverberk commented 9 years ago

This is a tricky one. We actually do use it on polymorphic relations but in our case we would have the searchabletrait on the details class. The path would start looping since the quoterequest would be pointing to itself every time. Not sure what the solution would be here...I'll take some time to think about it and see if I can come up with something.

mitranos commented 9 years ago

I dont want to bother, but I would like to know how to implement indexing for Polymorphic Relationships. I have the following Models:

class Event extends Model{
use SearchableTrait,

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

Then I have :

class Venue extends Model{
use TransformableTrait, CallableTrait;

 public function events()
    {
        return $this->morphMany('BeNight\Models\Event', 'eventable');
    }

}

class Promoter extends Model{
use TransformableTrait, CallableTrait;

 public function events()
    {
        return $this->morphMany('BeNight\Models\Event', 'eventable');
    }

}

How can I index the event model referencing Venue and Promoters? I have the same problem of @threesquared. Thanks for your time @iverberk