zoukankan      html  css  js  c++  java
  • whereHasIn方法

    model.php文件\

    use IlluminateDatabaseEloquentBuilder;
    use IlluminateDatabaseEloquentRelationsRelation;
    use IlluminateDatabaseEloquentRelationsBelongsTo;
    use IlluminateDatabaseEloquentRelationsHasOne;
    use IlluminateDatabaseEloquentRelationsHasMany;
    /**
    * whereHas 的 where in 实现
    * @param IlluminateDatabaseEloquentBuilder $builder
    * @param string $relationName
    * @param callable $callable
    * @return Builder
    *
    * @throws Exception
    */
    public function scopeWhereHasIn(Builder $builder, $relationName, callable $callable)
    {
    $relationNames = explode('.', $relationName);
    $nextRelation = implode('.', array_slice($relationNames, 1));

    $method = $relationNames[0];
    /** @var RelationsBelongsTo|RelationsHasOne $relation */

    $relation = Relation::noConstraints(function () use ($method) {
    return $this->$method();
    });

    /** @var Builder $in */
    if($nextRelation){
    $in = $relation->getQuery()->whereHasIn($nextRelation, $callable);
    } else {
    $in = $relation->getQuery()->where($callable);
    }
    if ($relation instanceof BelongsTo) {
    return $builder->whereIn($relation->getForeignKey(), $in->select($relation->getOwnerKey()));
    } elseif ($relation instanceof HasOne) {
    return $builder->whereIn($this->getKeyName(), $in->select($relation->getForeignKeyName()));
    } elseif ($relation instanceof HasMany){
    return $builder->whereIn($this->getKeyName(), $in->select($relation->getForeignKeyName()));
    }

    throw new Exception(__METHOD__ . " 不支持 " . get_class($relation));
    }

    使用 ----- User::where()->whereHasIn('goods', function($query){
    });

  • 相关阅读:
    python中的编码问题
    CVPR2018 Tutorial 之 Visual Recognition and Beyond
    hdu 1376 Octal Fractions
    hdu 1329 Hanoi Tower Troubles Again!
    hdu 1309 Loansome Car Buyer
    hdu 1333 Smith Numbers
    hdu 1288 Hat's Tea
    hdu 1284 钱币兑换问题
    hdu 1275 两车追及或相遇问题
    hdu 1270 小希的数表
  • 原文地址:https://www.cnblogs.com/JdsyJ/p/11051800.html
Copyright © 2011-2022 走看看