teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.1k stars 144 forks source link

Relational Search #37

Closed naumanahmed19 closed 7 years ago

naumanahmed19 commented 8 years ago

Hi, Thanks for this awesome package. Can you please tell me how to implement something like this I have 2 tables

Movies (hasManyActors) Actors (belongsToMovies)

now i want to search movies by actor name. I have read on forums that I may need to use egar load but how to implement this for larvel scout search?

Movies::whereHas('actors', function ($q) {
   $q->where('name', 'like', 'abc');
})->with('actors')->get();
naumanahmed19 commented 8 years ago

okay I tried algolia and this worked for me but wondering why its not working with tntsearch

    public function toSearchableArray()
    {
        $extra_data = [];
        $extra_data['actors'] = array_map(function ($data) {
            return $data['name'];
        }
        , $this->actors->toArray());

        return array_merge($this->toArray(), $extra_data);
    }
AdrianKuriata commented 8 years ago

Someone did it?

//Edit

This is will not working in array_merge, because this function do array into array, scout need string, not array, i mean need string into array, not array :dancer:

"mb_strtolower() expects parameter 1 to be string, array given"

I trying to do something with this, but i don't have idea.

blackfyre commented 7 years ago

The problem is that the search driver cannot process array type model attributes, only strings.

scottgrayson commented 7 years ago

Would this work? I'm attempting it now, but having other problems app/User.php:

    public function toSearchableArray()
    {
        // there is definitely a cleaner way to do this
        $search = collect($this->toArray())->only([
            'name',
            'email',
        ])->toArray();

        // relevant section for relations
        $search['address'] = isset($this->address)
            ? implode('', $this->address->toSearchableArray())
            : '';

        $search['profile'] = isset($this->profile)
            ? implode('', $this->profile->toSearchableArray())
            : '';

        return $search;
    }
breadlesscode commented 7 years ago
    public function toSearchableArray()
    {
        $this->project;
        $array = $this->toArray();
        $array['project_name'] = $array['project']['title'];
        unset($array['project']);
        return $array;
    }

I tryed this approach but if I search there is no project_name in my result.

Anyone solved this problem?

stokic commented 7 years ago

Did you reindex the data afterwards? If yes, how? What data do you have in your index after reindexing?

breadlesscode commented 7 years ago

I ran php artisan scout:flush App\\Models\\MyModel and then php artisan scout:import App\\Models\\MyModel.

I tried php artisan tntsearch:import App\\Models\\MyModel but this throws:

 [PDOException]                                                           
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'project_name' i  
  n 'field list'                                                           

php artisan scout:import App\\Models\\MyModel works but my result model has no project_name attribute.

Laravel: v5.3.28 laravel-scout-tntsearch-driver: v2.0.1

nticaric commented 7 years ago

Your result model won't have an project_name attribute since it doesn't exist on your model. It's still fetched as $model->project['title']

breadlesscode commented 7 years ago

Thanks! Yes you are right =)