pdphilip / laravel-elasticsearch

Laravel Elasticsearch: An Elasticsearch implementation of Laravel's Eloquent ORM
https://elasticsearch.pdphilip.com/
MIT License
95 stars 17 forks source link

fetch relations of model #8

Closed davoodf1995 closed 1 year ago

davoodf1995 commented 1 year ago

How can I get a relation to the search result ex: if I search on Account model i want to load related account_info as well , like Laravel eager loading

pdphilip commented 1 year ago

Hey @davoodf1995, can you provide examples of the query you're trying to build?

Also what the results were...

Thanks

davoodf1995 commented 1 year ago

Hey Thanks for the support my account model is `class Account extends BaseModel { protected $guarded = [];

public function products(): HasMany { return $this->hasMany(Product::class, 'account_id'); } `

I want when search some account and find it in my resualt search show products for this account

pdphilip commented 1 year ago

Hey @davoodf1995

In the interest of being useful, issues are geared towards giving the community a space to report a problem that they discovered, or to suggest a feature to move the project forward.

If you tried something with this plugin and it didn't work, then please share what you found.

Else, if you are not sure how to do something, documentation, and StackOverflow is your friend.

To answer your question anyway:

$account = Account::first();
 if ($account->products){
    foreach ($account->products as $product){
        //here are the related products      
    }    
}

With the plugin, you can have hybrid relationships across different data sources (MySQL and MongoDB), see: https://github.com/pdphilip/laravel-elasticsearch#relationships