thaoha / eloquent-search

Index Eloquent models to Elasticsearch
https://thaoha.github.io/eloquent-search/
MIT License
0 stars 1 forks source link

Feature request: index relationships #1

Open thijsmans opened 7 years ago

thijsmans commented 7 years ago

Thanks for a very nice simple and clean ES-client! Is there a way to index the relationships of my models, as if they were part of the model itself?

Example: model Book belongs to many Author's. Right now, the book is indexed but the author(s) is/are not. Authors could be indexed on their own, but not in relation to the books.

thaoha commented 7 years ago

Thank @thijsmans. I think you can use esIndex() for your relationship. Ex:

$book->esIndex();
$book->authors()->esIndex();

And you should be make sure your author model already use ElasticModelTrait.

thijsmans commented 7 years ago

Unfortunately that doesn't work:

$books = Book::all();
foreach( $books AS $book ) 
{
    $book->authors()->esIndex(); 
}

results in

BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::esIndex()'

Ofcourse you could do a foreach-loop on $book->authors() and esIndex() all authors by themselves, but that would just index the author by itself and not in relation to the book...

thaoha commented 7 years ago

Sorry @thijsmans. Please try: $book->authors->esIndex(); Because $book->authors return a collection but $book->authors() return a relationship object. And esIndex() function only work with collection object :D