Skip to content

Instantly share code, notes, and snippets.

@omurphy27
Last active July 10, 2017 13:38
Show Gist options
  • Save omurphy27/34edfd0a1e17393d830fcf9ab5e49ef3 to your computer and use it in GitHub Desktop.
Save omurphy27/34edfd0a1e17393d830fcf9ab5e49ef3 to your computer and use it in GitHub Desktop.
Laravel 5.2 - Eloquent query with nested Where and orWhere like variables
<?php
$results = ClinicalStudy::where( function( $query ) use ( $data )
{
$query->where('brief_title', 'like', $data['query'] .'%')
->orWhere('source', 'like', $data['query'] .'%')
->orWhere('brief_summary', 'like', $data['query'] .'%')
->orWhere('detailed_description', 'like', $data['query'] .'%');
});
if ( $data['overall_status'] ) {
$results = $results->where('overall_status', $data['overall_status']);
}
if ( $data['gender'] ) {
$results = $results->where('gender', $data['gender']);
}
$output['count'] = $results->count();
$output['hits'] = $results
->skip( $offset )
->take( intval( $data['hitsPerPage'] ) )
->get([
'nct_id',
'brief_title',
'source',
'brief_summary',
'detailed_description',
'overall_status',
'start_date',
'gender',
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment