# Search Repository

This helps return response for records in a database. It will help with important actions like search, order by and pagination out of the box.

All records to be displayed with sh-table vue component should be returned using this method.

After running your eloquent query, here is how to return the data. Kindly note that query and not the data is passed to the search repositiory.

This assumes that you have imported the search repo

use App\Repositories\SearchRepo;

After importing it, in your controller function, here is an example on how to use it.

$user = \request()->user();
$tasks = Task::join('users','users.id','=','tasks.user_id')
->select('tasks.*','users.name as user');
$search_keys = ['name','description','users.name'];
return[
    'status'=>'success',
    'data'=>SearchRepo::of($tasks,'tasks',$search_keys)
    ->addColumn('status', function ($task){
        return $task->status == 1 ? 'active':'completed';
    })
        ->make(true)
];

From the above example, we can see that search repo takes 3 parameters Eloquent query, table and search keys. Eloquent query is the model query without finishing with get or paginate, Table is table name of where we did select *, other fields like name in search fields will automatically be appended with table name like name will be converted to tasks.name.