/** * Create a full attachment record payload. * * This includes the from_field_id in the attachment payload * * @param int $key * @param mixed $value * @param array $attributes * @param bool $timed * @return array */ protected function attacher($key, $value, $attributes, $timed) { if (empty($attributes)) { $attributes = [static::$fromFieldKey => $this->fromFieldId]; } return parent::attacher($key, $value, $attributes, $timed); }
/** * Sync the intermediate tables with a list of IDs or collection of models. * * @param array $ids * @param bool $detaching * * @return array */ public function sync($ids, $detaching = true) { // @todo // Instead of using a pre and post sync, we can rely on the array // returned in $changes if this PR gets merged into Laravel // https://github.com/laravel/framework/pull/10100 $this->parent->preSync($this->relationName); $changes = parent::sync($ids, $detaching); $this->parent->postSync($this->relationName, $ids); return $changes; }
/** * Attach 3 models. * * @param mixed $id * @param array $attributes * @param boolean $touch * * @return void */ public function attach($id, array $attributes = array(), $touch = true) { // First check if developer provided an array of keys or models to attach // and set other key as additional pivot data for generic attach method // in order to make sure it is always saved upon attaching if provided. if (is_array($id) && count($id) > 1) { $otherId = $id[1] instanceof Model ? $id[1]->getKey() : $id[1]; $id = $id[0] instanceof Model ? $id[0]->getKey() : $id[0]; $attributes[$this->thirdKey] = $otherId; } return parent::attach($id, $attributes, $touch); }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ public function get($columns = array('*')) { if (empty($this->getParent()->getKey())) { return parent::get($columns); } $models = $this->related->whereIn($this->getForeignKey(), $this->getParent()->getKey())->getModels(); $this->hydratePivotRelation($models); if (count($models) > 0) { $models = $this->query->eagerLoadRelations($models); } return $this->related->newCollection($models); }
/** * Uploads and attaches files for the current relation. * * @param Filesystem $filesystem * * @return bool|array */ public function handle(Filesystem $filesystem) { $files = $this->request->file('files'); if (is_array($files)) { $uploaded = []; foreach ($files as $file) { // Double check that we have an uploaded file instance. if ($file instanceof UploadedFile) { // Generates the unique file name. $name = implode('.', [uuid(), $file->getClientOriginalExtension()]); // Generates the complete storage path. $path = implode(DIRECTORY_SEPARATOR, [$this->path, $name]); // Try and move the uploaded file into storage. if ($filesystem->put($path, file_get_contents($file->getRealPath()))) { // Successfully moved uploaded file, create the record. $attributes = ['user_id' => auth()->id(), 'name' => $file->getClientOriginalName(), 'file_name' => $name, 'file_path' => $path]; $uploaded[] = $this->relation->create($attributes); } } } return $uploaded; } return false; }
/** * Updates an upload record for the specified work order. * * @param AttachmentUpdateRequest $request * @param BelongsToMany $relation * @param int|string $id * * @return \Stevebauman\Maintenance\Models\Attachment|bool */ public function update(AttachmentUpdateRequest $request, BelongsToMany $relation, $id) { $attachment = $relation->find($id); if ($attachment) { $attachment->name = $request->input('name', $attachment->name); if ($attachment->save()) { return $attachment; } } return false; }
/** * Create a new belongs to many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $table * @param string $foreignKey * @param string $otherKey * @param string $relationName * @return void */ public function __construct(Builder $query, Model $parent, $table, $foreignKey, $otherKey, $relationName = null) { parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName); $this->addDefinedConstraints(); }
/** * Create a new query builder for the pivot table. * * @return \Illuminate\Database\Query\Builder */ protected function newPivotQuery() { $query = parent::newPivotQuery(); return $query->where($this->morphType, $this->morphClass); }
protected function joinManyToManyRelation(Relations\BelongsToMany $relation, $type) { $pivotTable = $relation->getTable(); // $relation->getQualifiedParentKeyName() is protected $parentKey = $relation->getParent()->getQualifiedKeyName(); $localKey = $relation->getOtherKey(); $this->query->join($pivotTable, $localKey, '=', $parentKey, $type); $related = $relation->getRelated(); $foreignKey = $relation->getForeignKey(); $relatedTable = $related->getTable(); $relatedKey = $related->getQualifiedKeyName(); $this->query->join($relatedTable, $foreignKey, '=', $relatedKey, $type); }
private function generateJoinFromBelongsToMany(BelongsToMany $relation) { // Pivot Table Generated On $generatedOnClause = [[$relation->getForeignKey(), $relation->getQualifiedParentKeyName()]]; // Pivot Table Join $this->generateJoin($relation->getTable(), $generatedOnClause); // Related Table Generated On $generatedOnClause = [$this->related->getQualifiedKeyName(), $relation->getOtherKey()]; // Related Table Join $this->generateJoin($this->related->getTable(), $this->compileOns($generatedOnClause), $this->nested ? $this->conditions[$this->index] : $this->conditions); }
/** * Attach a model to the parent. * * @param mixed $id * @param array $attributes * @param bool $touch */ public function attach($id, array $attributes = [], $touch = true) { $attributes[$this->getOrderColumnName()] = $this->getNextPosition(); parent::attach($id, $attributes, $touch); }
/** * Init nested query for filter. * * @param QueryBuilder $query * @param array $ids * * @return void */ protected function initNestedQuery(QueryBuilder $query, array $ids) { $connection = $query->getConnection(); $keyName = $connection->raw($this->relation->getParent()->getQualifiedKeyName()); $query->from($this->relation->getTable())->select($connection->raw('1'))->where($this->relation->getForeignKey(), '=', $keyName)->whereIn($this->relation->getOtherKey(), $ids); }
/** * Detach models from the relationship. * * @param array $ids * @param bool $touch * @return int|void */ public function detach($ids = [], $touch = true) { if ($ids instanceof Model) { $ids = (array) $ids->getKey(); } $query = $this->newPivotQuery(); // If associated IDs were passed to the method we will only delete those // associations, otherwise all of the association ties will be broken. // We'll return the numbers of affected rows when we do the deletes. $ids = (array) $ids; if (count($ids) > 0) { $values = $query->whereIn($this->otherKey, (array) $ids)->get(); } parent::detach($ids, $touch); // TODO: Change the autogenerated stub if (isset($values) && count($values) > 0) { //the get method above, returns std objects, so we quickly map them. $values = array_map(function ($item) { return (array) $item; }, $values); //fire based on the values, since the $ids can still contain invalid data. foreach ($values as $value) { app('events')->fire('eloquent.detached: ' . $this->relationName, [$value]); } } }
protected function setJoin($query = null) { $query = $query ?: $this->query; return parent::setJoin($query instanceof EloquentBuilder ? $query->getQuery() : $query); }
protected function applyBelongsToMany(Query $query, Model $model, BelongsToMany $belongsToMany, $name) { if (isset($this->joinClasses[$name])) { return; } $modelTable = $model->getTable(); $related = $belongsToMany->getRelated(); $pivotTable = $belongsToMany->getTable(); $pivotAlias = $pivotTable . '_pivot'; $pivotLocalKey = $belongsToMany->getOtherKey(); $relatedTable = $related->getTable(); $relationKey = $related->getKeyName(); $foreignKey = $belongsToMany->getForeignKey(); $qualifiedLocalKey = $belongsToMany->getQualifiedParentKeyName(); $alias = $this->joinNameToAlias($name); $joinMethod = $this->getJoinMethod($name); $query->{$joinMethod}("{$pivotTable}", "{$qualifiedLocalKey}", '=', "{$foreignKey}"); $query->{$joinMethod}("{$relatedTable} AS {$alias}", "{$relatedTable}.{$relationKey}", '=', "{$pivotLocalKey}"); $query->distinct(); $this->joinClasses[$name] = $related; $this->joinTable[$name] = $relatedTable; $this->joinAliases[$name] = $alias; }
/** * Create a new belongs to many relationship instance. * Sets default ordering by $orderColumn column. * * @param Builder $query * @param Model $parent * @param string $table * @param string $foreignKey * @param string $otherKey * @param string $relationName * @param string $orderColumn position column name */ public function __construct(Builder $query, Model $parent, $table, $foreignKey, $otherKey, $relationName = null, $orderColumn = null) { parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName); $this->setOrderColumn($orderColumn); }