Skip to content

Instantly share code, notes, and snippets.

Created July 26, 2016 20:44
Show Gist options
  • Save anonymous/0eb25d2fe5f3b702e9096c4ec2ac65e8 to your computer and use it in GitHub Desktop.
Save anonymous/0eb25d2fe5f3b702e9096c4ec2ac65e8 to your computer and use it in GitHub Desktop.
filter
Post::whereHas('tags', function ($query) use ($t) {
$query->where('tag_id','=', $t);
})->whereHas('languages', function ( $query ) use ( $l ){
$query->where('language_id', '=',$l);
})->orWhere(function ($q) use ($search) {
$q->where('title', 'LIKE', '%' . $search . '%')
->orWhere('explanation','LIKE','%'.$search.'%')
->orWhere('usage','LIKE','%'.$search.'%')
->orWhere('codeexample','LIKE','%'.$search.'%')
->orderBy('id','desc');
})->get();
@kemalkanok
Copy link

kemalkanok commented Jul 26, 2016

$collection = app(Post::class);
//if tags

$collection = $collection->
 whereHas('tags', function ($query) use ($t) {
            $query->where('tag_id','=', $t);
        });
//if languages
 $collection = $collection->whereHas('languages', function ( $query ) use ( $l ){
            $query->where('language_id', '=',$l);
        });
//if search
 $collection = $collection->where(function ($q) use ($search) {
            $q->where('title', 'LIKE', '%' . $search . '%')
                ->orWhere('explanation','LIKE','%'.$search.'%')
                ->orWhere('usage','LIKE','%'.$search.'%')
                ->orWhere('codeexample','LIKE','%'.$search.'%')
                ->orderBy('id','desc');
});
//get all
 $collection = $collection -> get();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment