// $_GET Data http://myapp.dev?created=&group=&maxAge=70&minAge=14&pics=&postTown=All&q=carter&sort=dob&status= // Controller public function getSearch() { $input = Request::all(); if ( empty( $input ) ) { return $this->view( 'matchmaker::search/search-results' ); } else { $applications = $this->applicant->search( $input )->with( 'applicant', 'applicant.interviews' )->get(); return $this->view( 'matchmaker::search/search-results', compact( [ 'applications' ] ) ); } } // Repository public function search( $request ) { $results = $this->application->search( $request['q'] ); foreach ( array_except( $request, [ 'q', 'sort' ] ) as $key => $value ) { if ( isset( $value ) ) { $method = 'scope' . ucfirst( $key ); if ( method_exists( $this->model, $method ) ) { $results->{$key}( $request[ $key ] ); } } } return $results; } // Model public function scopeSearch( $query, $search ) { // Handle any search term parameters $params = explode( ",", $search ); return $query->where( function ( $q ) use ( $params ) { foreach ( $params as $param ) { $q->where( 'company', 'LIKE', "%" . $param . "%" ) ->orWhere( 'page_title', 'LIKE', "%" . $param . "%" ) ->orWhereHas( 'applicant', function ( $q ) use ( $param ) { $q->where( DB::raw( 'CONCAT(LOWER(TRIM(fname)), LOWER(TRIM(surname)))' ), 'LIKE', "%" . strtolower( str_replace( ' ', '', $param ) ) . "%" ); } ); } } ); } /** * -------------------------------------------------------------------------------------- * Method sorts the (attempts to) collection * -------------------------------------------------------------------------------------- */ public function scopeSort( $query, $orderBy ) { if ( $orderBy == 'dob' ) { return $query->orderBy( 'applicant.dob' ); } elseif ( $orderBy = 'created_at' ) { return $query->orderBy( 'created_at', 'desc' ); } else { return $query->orderBy( 'applicant.surname' ); } } public function scopeCreated( $query, $date ) { if ( $date != '' ) { return $query->where( 'created_at', '>=', Carbon::createFromFormat( 'd/m/Y', $date )->format( 'Y-m-d' ) . ' 00:00:00' ); } } public function scopeMaxAge( $query, $maxAge ) { if ( $maxAge != '' ) { return $query->whereHas( 'applicant', function ( $q ) use ( $maxAge ) { $q->hasDob()->where( 'dob', '>=', Carbon::now()->subYears( $maxAge + 1 ) ); } ); } } public function scopeMinAge( $query, $minAge ) { if ( $minAge != '' ) { return $query->whereHas( 'applicant', function ( $q ) use ( $minAge ) { $q->hasDob()->where( 'dob', '<=', Carbon::now()->subYears( $minAge ) ); } ); } } public function scopePics( $query, $pics ) { // Only returns learners that have been on PICS if ( $pics == 'true' ) { return $query->whereHas( 'applicant', function ( $q ) use ( $pics ) { $q->whereNotNull( 'client_id' )->where( 'client_id', '>', 0 ); } ); } // Return everyone except learners on PICS if ( $pics == 'false' ) { return $query->whereHas( 'applicant', function ( $q ) use ( $pics ) { $q->where( 'client_id', 0 ); } ); } } public function scopePostTown( $query, $town ) { if ( $town != 'All' ) { return $query->whereHas( 'applicant', function ( $q ) use ( $town ) { $q->hasDob()->where( 'postTown', $town ); } ); } } public function scopeStatus( $query, $status ) { if ( $status != '' ) { return $query->whereHas( 'applicant', function ( $q ) use ( $status ) { $q->where( 'contact_status_id', $status ); } ); } } public function scopeGroup( $query, $group ) { if ( $group == 'email' ) { return $query->groupBy( 'email' ); } }