/** * @param Relation $relation * @param array $excluded_ids */ public static function deleteAllRelatedExcept(Relation $relation, $excluded_ids = []) { $related = $relation->getRelated(); $key_name = $related->getKeyName(); $query = $relation->getQuery(); if (count($excluded_ids) > 0) { $query->whereNotIn($key_name, $excluded_ids); } $query->delete(); }
/** * Sets up the existing relationship wheres. * * @param \Illuminate\Database\Eloquent\Relations\Relation $relationship * @param string $tableAlias * @param string $pivotAlias * @param string $pivot * * @return string */ public function getRelationshipWheres($relationship, $tableAlias, $pivotAlias = null, $pivot = null) { //get the relationship model $relationshipModel = $relationship->getRelated(); //get the query instance $query = $relationship->getQuery()->getQuery(); //get the connection instance $connection = $query->getConnection(); //one element of the relationship query's wheres is always useless (it will say pivot_table.other_id is null) //depending on whether or not softdeletes are enabled on the other model, this will be in either position 0 //or 1 of the wheres array array_splice($query->wheres, method_exists($relationshipModel, 'getDeletedAtColumn') ? 1 : 0, 1); //iterate over the wheres to properly alias the columns foreach ($query->wheres as &$where) { //alias the where columns $where['column'] = $this->aliasRelationshipWhere($where['column'], $tableAlias, $pivotAlias, $pivot); } $sql = $query->toSql(); $fullQuery = $this->interpolateQuery($sql, $connection->prepareBindings($query->getBindings())); $split = explode(' where ', $fullQuery); return isset($split[1]) ? $split[1] : ''; }
/** * Add the "has" condition where clause to the query. * * @param \Illuminate\Database\Eloquent\Builder $hasQuery * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param string $operator * @param int $count * @param string $boolean * @return \Illuminate\Database\Eloquent\Builder|static */ protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator, $count, $boolean) { $hasQuery->mergeModelDefinedRelationConstraints($relation->getQuery()); if ($this->shouldRunExistsQuery($operator, $count)) { $not = $operator === '<' && $count === 1; return $this->addWhereExistsQuery($hasQuery->toBase(), $boolean, $not); } return $this->whereCountQuery($hasQuery->toBase(), $operator, $count, $boolean); }