Skip to content

Instantly share code, notes, and snippets.

@Tucker-Eric
Created November 11, 2016 20:10
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 Tucker-Eric/806a1d18e7547a24ded957bf2516b084 to your computer and use it in GitHub Desktop.
Save Tucker-Eric/806a1d18e7547a24ded957bf2516b084 to your computer and use it in GitHub Desktop.
Laravel Eloquent Model Trait for querying based on a relationship and including that relationship with the constraints with the results
<?php
namespace App\Models\Traits;
trait WhereHasWithTrait
{
public function scopeWhereHasWith($query, $relation, \Closure $closure)
{
return $query->whereHas($relation, $closure)->with([$relation => $closure]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment