public function searchByTerm($table, $searchTerm, $limit, $searchInAllColumns = true, $columns = array(), $excludeColumns = array(), $orderBy = 'created_at', $orderType = 'DESC', $joins = array(), $conditions = array())
    {
        $langSlug = App::getLocale();

        if($searchInAllColumns && !$columns) {
            $columns = DB::connection($this->DATABASE_CONNECTION)->select(DB::connection($this->DATABASE_CONNECTION)->raw("SHOW COLUMNS FROM $table FROM ".DB::getDatabaseName()));
            if(!$columns) {
                throw new \Exception("No columns could be found for table:".$table);
            }
        }
        // get table
        $queryObject = DB::connection($this->DATABASE_CONNECTION)->table($table);

        // use where clause for each column
        foreach ($columns as $column){
            if ($column->Field != 'password') {
                if(!in_array($column->Field, $excludeColumns)) {
                    if ($searchInAllColumns) {
                        if ($column->Type == "json") {
                            $queryObject->orWhere($column->Field.'->'.$langSlug, 'like', '%'.$searchTerm.'%');
                        }else{
                            $queryObject->orWhere($column->Field, 'like', '%'.$searchTerm.'%');
                        }
                    }else{
                        $queryObject->orWhere($column->Field, 'like', '%'.$searchTerm.'%');
                    }
                }

                if($conditions) {
                    foreach ($conditions as $condition){
                        $method = $condition[0];
                        $queryObject->$method($condition[1], $condition[2], $condition[3]);
                    }
                }
            }
        }

        if($joins != '') {
            foreach ($joins as $join){
                if($join['type'] == 'left') { // if it is a left join
                    $queryObject->leftJoin($join['table'], $table.'.'.$join['whereTable1'], '=', $join['table'].'.'.$join['whereTable2']);
                }else if($join['type'] == 'inner') { // if it is a inner join
                    $queryObject->join($join['table'], $table.'.'.$join['whereTable1'], '=', $join['table'].'.'.$join['whereTable2']);
                }
            }
        }

        return $queryObject->orderBy($orderBy, $orderType)
            ->paginate($limit);
    }
public function searchByTerm($table, $searchTerm, $limit, $searchInAllColumns = true, $columns = array(), $excludeColumns = array(), $orderBy = 'created_at', $orderType = 'DESC', $joins = array(), $conditions = array())
    {
        $langSlug = App::getLocale();

        if($searchInAllColumns && !$columns) {
            $columns = DB::connection($this->DATABASE_CONNECTION)->select(DB::connection($this->DATABASE_CONNECTION)->raw("SHOW COLUMNS FROM $table FROM ".DB::getDatabaseName()));
            if(!$columns) {
                throw new \Exception("No columns could be found for table:".$table);
            }
        }
        // get table
        $queryObject = DB::connection($this->DATABASE_CONNECTION)->table($table);

        // use where clause for each column
        foreach ($columns as $column){
            if ($column->Field != 'password') {
                if(!in_array($column->Field, $excludeColumns)) {
                    if ($searchInAllColumns) {
                        if ($column->Type == "json") {
                            $queryObject->orWhere($column->Field.'->'.$langSlug, 'like', '%'.$searchTerm.'%');
                        }else{
                            $queryObject->orWhere($column->Field, 'like', '%'.$searchTerm.'%');
                        }
                    }else{
                        $queryObject->orWhere($column->Field, 'like', '%'.$searchTerm.'%');
                    }
                }

                if($conditions) {
                    foreach ($conditions as $condition){
                        $method = $condition[0];
                        $queryObject->$method($condition[1], $condition[2], $condition[3]);
                    }
                }
            }
        }

        if($joins != '') {
            foreach ($joins as $join){
                if($join['type'] == 'left') { // if it is a left join
                    $queryObject->leftJoin($join['table'], $table.'.'.$join['whereTable1'], '=', $join['table'].'.'.$join['whereTable2']);
                }else if($join['type'] == 'inner') { // if it is a inner join
                    $queryObject->join($join['table'], $table.'.'.$join['whereTable1'], '=', $join['table'].'.'.$join['whereTable2']);
                }
            }
        }

        return $queryObject->orderBy($orderBy, $orderType)
            ->paginate($limit);
    }
public function searchByTerm ( $ table , $ searchTerm , $ limit , $ searchInAllColumns = true , $ columns = array ( ) , $ excludeColumns = array ( ) , $ orderBy = 'created_at' , $ orderType = 'DESC' , $ joins = array ( ) , $ conditions = array ( ) ) { $ langSlug = App :: getLocale ( ) ; if ( $ searchInAllColumns && ! $ columns ) { $ columns = DB :: connection ( $ this -> DATABASE_CONNECTION ) -> select ( DB :: connection ( $ this -> DATABASE_CONNECTION ) -> raw ( "SHOW COLUMNS FROM $table FROM " . DB :: getDatabaseName ( ) ) ) ; if ( ! $ columns ) { throw new \ Exception ( "No columns could be found for table:" . $ table ) ; } } // get table $ queryObject = DB :: connection ( $ this -> DATABASE_CONNECTION ) -> table ( $ table ) ; // use where clause for each column foreach ( $ columns as $ column ) { if ( $ column -> Field != 'password' ) { if ( ! in_array ( $ column -> Field , $ excludeColumns ) ) { if ( $ searchInAllColumns ) { if ( $ column -> Type == "json" ) { $ queryObject -> orWhere ( $ column -> Field . '->' . $ langSlug , 'like' , '%' . $ searchTerm . '%' ) ; } else { $ queryObject -> orWhere ( $ column -> Field , 'like' , '%' . $ searchTerm . '%' ) ; } } else { $ queryObject -> orWhere ( $ column -> Field , 'like' , '%' . $ searchTerm . '%' ) ; } } if ( $ conditions ) { foreach ( $ conditions as $ condition ) { $ method = $ condition [ 0 ] ; $ queryObject -> $ method ( $ condition [ 1 ] , $ condition [ 2 ] , $ condition [ 3 ] ) ; } } } } if ( $ joins != '' ) { foreach ( $ joins as $ join ) { if ( $ join [ 'type' ] == 'left' ) { // if it is a left join $ queryObject -> leftJoin ( $ join [ 'table' ] , $ table . '.' . $ join [ 'whereTable1' ] , '=' , $ join [ 'table' ] . '.' . $ join [ 'whereTable2' ] ) ; } else if ( $ join [ 'type' ] == 'inner' ) { // if it is a inner join $ queryObject -> join ( $ join [ 'table' ] , $ table . '.' . $ join [ 'whereTable1' ] , '=' , $ join [ 'table' ] . '.' . $ join [ 'whereTable2' ] ) ; } } } return $ queryObject -> orderBy ( $ orderBy , $ orderType ) -> paginate ( $ limit ) ; }
Search data by a term

@param string  $table              Name of DB Table
@param string  $searchTerm         Search Query
@param int     $limit              Number of results that should be shown in a single page
@param boolean $searchInAllColumns Search in all columns of the table, if false,
@param array   $columns            List of columns to search in
@param array   $excludeColumns     List of columns to exclude from search
@param string  $orderType          How should we order data, ASC or DESC
@param string  $orderBy            By which column should results be ordered
@param array   $joins              = List of table to join, ex.
$join = array( [ 'table' =>
'media', 'type' => 'left',
'whereTable1' =>
"profileImageID",
'whereTable2' => "mediaID",
] );
@param array   $conditions         Conditions to search in table. It stimulates laravel's where() function, ex. [["where","id","=",2],["where","status","=","published"]]

@return Pagination::make

@throws Exception If no columns could be found
Search data by a term