laravel-scout-tntsearch-driver icon indicating copy to clipboard operation
laravel-scout-tntsearch-driver copied to clipboard

Return specific columns

Open oliviertam opened this issue 8 years ago • 3 comments

Hello, i want to make this query:

$persons = Person::search($search_value)->get(['id', 'first_name]);

Can anyone tell me if this is possible?

oliviertam avatar Jan 19 '17 08:01 oliviertam

Hi,

$persons = Person::search($search_value)->pluck('id','name); ? return a collection, so add ->toArray() method at the end if you want an array :)

simpledev avatar Feb 03 '17 17:02 simpledev

I've done this…

    $companies = Company::search($searchString)->get();
    $companyids = array();
    foreach ($companies as $company) {
        $companyids[] = $company->id;
    }

I'm sure there is a better way but it works

garymtate avatar Nov 02 '17 16:11 garymtate

For anyone still interested, you can pass a callback function to the Scout Builder instance.

    $columns = ['id', 'name'];
    Person::search($searchString)->query(function($query) use ($columns) { 
       $query->select($columns);  
    })->get();

chribell avatar Jan 14 '20 15:01 chribell