searchable icon indicating copy to clipboard operation
searchable copied to clipboard

Change Columns and Joins Dynamically [SOLVED]

Open trijulio opened this issue 5 years ago • 0 comments

If you want to change the columns to search on the same model, you can do it dynamically inserting this code to your model or to the source file:

vendor/nicolaslopezj/searchable/src/SearchableTrait.php

    public function scopeSetSearchScope($query,$search = []){
        $this->searchable = $search;
        return $query;
    }

Then you can:

        $toSearch = [
            'columns' => [
                'contacts.name' => 10,
                'contacts.last_name' => 10,
                'contacts.alias' => 10,
                'fields.value' => 5,
            ],
            'joins' => [
                'fields' => ['contacts.id','fields.contact_id'],
            ],
        ];

        $contacts =  Contact::with(['fields'])
                    ->companyScope()
                    ->setSearchScope($toSearch)
                    ->search(request('search'),null,true)
                    ->groupBy('contacts.id')
                    ->orderBy('relevance','desc')
                    ->paginate(request('show',100));

trijulio avatar Feb 02 '20 05:02 trijulio