Skip to content

Instantly share code, notes, and snippets.

@Oldenborg
Last active September 27, 2016 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oldenborg/6d74eb3b68840eb2c68f2ad2991aa220 to your computer and use it in GitHub Desktop.
Save Oldenborg/6d74eb3b68840eb2c68f2ad2991aa220 to your computer and use it in GitHub Desktop.
Laravel - Get Model with specific relations
<?php
$clinics = new Clinic;
if($request->modules)
{
foreach($request->modules as $module)
{
$clinics = $clinics->whereHas('modules', function ($query) use ($module) {
$query->where('modules.id',$module);
});
}
}
if($request->services)
{
foreach($request->services as $service)
{
$clinics = $clinics->whereHas('services', function ($query) use ($service) {
$query->where('services.id',$service);
});
}
}
$filteredClinics = $clinics->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment