Example of Global scopes in OpenLitterMap/openlittermap-web

File app/Models/User/User.php (link to Github)
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    //
    public static function boot ()
    {
        //
        static::addGlobalScope('photosCount', function($builder) {
            $builder->withCount('photos'); // photos_count
        });
    }
    //
}

Additional resources on global scopes: