public function createQuestion(Model $questionable, $data, Model $author) { $question = new static(); $question->fill(array_merge($data, ['author_id' => $author->id, 'author_type' => get_class($author)])); $questionable->questions()->save($question); return $question; }
/** * Delete the given model entity. * * @param Model $model Model to be deleted. * @param string $msg Message for a successful delete. * @param string $title Title for a successful delete. * * @return array */ protected function deleteThe(Model $model, $msg = 'messages.deleted', $title = 'messages.success') { if ($model->delete()) { return ['title' => trans("admin::{$title}"), 'msg' => trans("admin::{$msg}")]; } return reportError(); }
/** * Attach a model instance to the parent model. * * This adds the field_id value * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model) { if ($this->fieldId) { $model->setAttribute($this->fieldKey, $this->fieldId); } return parent::save($model); }
/** * Create a new morph to many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $name * @param string $table * @param string $foreignKey * @param string $otherKey * @param string $relationName * @param bool $inverse * @return void */ public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false) { $this->inverse = $inverse; $this->morphType = $name . '_type'; $this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName); }
/** * Update a user. * * @param Model $item * @param array $data * @return bool|int */ public function update(Model $item, array $data) { if (isset($data['password'])) { $data['password'] = bcrypt($data['password']); } return $item->update($data); }
/** * @param Model $reviewable * @param $data * @param Model $author * * @return static */ public function createReview(Model $reviewable, $data, Model $author) { $review = new static(); $review->fill(array_merge($data, ['author_id' => $author->id, 'author_type' => get_class($author)])); $reviewable->reviews()->save($review); return $review; }
/** * Attaches a relationship to the model * * @param String $relationshipName Name of relationship in request * @param Model $model Model that has the relationships * @param mixed $relationshipData Model(s) to associate * * @return Model */ public function attachRelationship($relationshipName, Model $model, $relationshipData) { if ($relationshipName === 'next') { $model->pipeable()->associate($relationshipData); } return $model; }
/** * Import an Eloquent * * @param Model $model * @param array $relations * @param int $batchSize * @param callable $callback * @internal param $type */ public function import(Model $model, $relations = [], $batchSize = 750, callable $callback = null) { $batch = 0; $asQueryLoggind = $model->getConnection()->logging(); $model->getConnection()->disableQueryLog(); while (true) { // Increase the batch number $batch += 1; // Load records from the database $records = $model->newInstance()->with($relations)->skip($batchSize * ($batch - 1))->take($batchSize)->get(); // Break out of the loop if we are out of records if (count($records) == 0) { break; } // Call the callback function to provide feedback on the import process if ($callback) { $callback($batch); } // Transform each record before sending it to Elasticsearch $data = []; foreach ($records as $record) { $data[] = ['index' => ['_id' => $record->getEsId()]]; $data[] = $record->transform(!empty($relations)); } // Bulk import the data to Elasticsearch $this->bulk($data); } if ($asQueryLoggind) { $model->getConnection()->enableQueryLog(); } }
/** * Aggregates data handling to the subclasses. * * @param array $data the handling internediate data. * @param array|Model $value the handling Model instance. * @return array the resulting intermediate Format instance. */ protected function aggregate(array $data, Model $value) { $data[self::KEY_OF_TABLE_NAME] = $value->getTable(); $data[self::KEY_OF_FOREIGN_KEY] = $value->getForeignKey(); $data[self::KEY_OF_OTHER_KEY] = $value->getOtherKey(); return $data; }
/** * Merges EAV "values" into the entity model, for easy access and * manipulation. * * @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Collection $collection * @return void */ protected function mergeValues(Model $model, Collection $values) { foreach ($values as $value) { $attribute = $value->getRelation($value->getAttributeRelation()); $model->setAttribute($attribute->getAttributeKey(), $value->getValueKey()); } }
/** * Defer loading of Collection until needed * @return void */ protected function boot() { if (!is_null($this->collection)) { return; } $this->collection = $this->model->all(); }
/** * Get an Item from storage with optional relations * * @param $id * @param array $with * @return Item */ public function get($id, $with = []) { if (count($with)) { return $this->model->where('id', '=', $id)->with($with)->first(); } return $this->model->find($id); }
/** * Store Detail vehicle * * @param Request $request * @param Model|Header $header * @param bool $coverage * * @return bool */ public function storeVehicle($request, $header, $coverage = false) { $this->data = $request->all(); try { if ($this->data['year'] === 'old') { $this->data['year'] = $this->data['year_old']; } $id = date('U'); $detail = ['id' => $id, 'ad_vehicle_type_id' => $this->data['vehicle_type']['id'], 'ad_vehicle_make_id' => $this->data['vehicle_make']['id'], 'ad_vehicle_model_id' => $this->data['vehicle_model']['id'], 'ad_retailer_product_category_id' => $this->data['category']['id'], 'year' => $this->data['year'], 'license_plate' => $this->data['license_plate'], 'use' => $this->data['use'], 'mileage' => (bool) $this->data['mileage'], 'insured_value' => $this->data['insured_value']]; if ($coverage) { $detail['color'] = $this->data['color']; $detail['engine'] = $this->data['engine']; $detail['chassis'] = $this->data['chassis']; $detail['tonnage_capacity'] = $this->data['tonnage_capacity']; $detail['seat_number'] = $this->data['seat_number']; } $header->details()->create($detail); if ($coverage && $this->getDetailById($id)) { return true; } return true; } catch (QueryException $e) { $this->errors = $e->getMessage(); } return false; }
/** * @param Model $ratingable * @param $data * @param Model $author * * @return static */ public function createRating(Model $ratingable, $data, Model $author) { $rating = new static(); $rating->fill(array_merge($data, ['author_id' => $author->id, 'author_type' => get_class($author)])); $ratingable->ratings()->save($rating); return $rating; }
/** * @param $model * @param $tagArray [Input::get('tag') ] * update the taggables */ public function attachTags(Model $model, array $tagArray) { // attach related tags // fetch all tags $tags = $model->tags; // fetch all tags in the database assosiated with this event $attachedTags = $tags->lists('id'); if (!empty($attachedTags)) { // if there are any tags assosiated with the event if (empty($tagArray)) { // if no tags in the GET REQUEST, delete all the tags foreach ($attachedTags as $tag) { // delete all the tags $model->tags()->detach($tag); } } else { // If the used tags is unselected in the GET REQUEST, delete the tags foreach ($attachedTags as $tag) { if (!in_array($tag, $tagArray)) { $model->tags()->detach($tag); } } } } // attach the tags if (!empty($tagArray)) { $model->tags()->sync($tagArray, true); } }
/** * Initialize * * @return void */ public function initialize() { if (!$this->initialized) { $this->prototype = $this->model->newFromBuilder(); } $this->initialized = true; }
/** * @param Model $model * @param $column * @return mixed */ protected function authorizeForResults(Model $model, $column) { if (!array_key_exists($column, $model->toArray())) { return $this->authorize('add_activity', $model); } return $this->authorize('edit_activity', $model); }
/** * 删除资源 * * @param \Illuminate\Database\Eloquent\Model $model * * @return mixed * @throws \Exception */ public function destroy(Model $model) { if ($model->delete()) { return $this->success('删除成功'); } return $this->error('删除失败'); }
/** * Seed the form with defaults that are stored in the session * * @param Model $model * @param CrudController $crudController */ public function onCrudSaved(Model $model, CrudController $crudController) { $fb = $crudController->getFormBuilder(); foreach ($fb->getElements() as $name => $element) { if (!$element instanceof FileElement) { continue; } if ($model instanceof File) { $file = $model; } else { $file = new File(); } if ($uploaded = Input::file($name)) { // Save the file to the disk $uploaded->move(storage_path($element->getPath()), $uploaded->getClientOriginalName()); // Update the file model with metadata $file->name = $uploaded->getClientOriginalName(); $file->extension = $uploaded->getClientOriginalExtension(); $file->size = $uploaded->getClientSize(); $file->path = $element->getPath() . '/' . $uploaded->getClientOriginalName(); $file->save(); if (!$model instanceof File) { $model->{$name} = $element->getPath() . '/' . $uploaded->getClientOriginalName(); $model->save(); } } } }
public function created(Model $model) { if ($model->hashidStrategy() == 'id') { $model->generateHashidFromId(); $model->save(); } }
public function __construct(\Illuminate\Database\Eloquent\Model $extra) { $this->common = $extra->toArray(); $this->composition = $extra->composition->toArray(); $this->atmosphere = $extra->atmosphere->toArray(); $this->orbit = $extra->orbit->toArray(); }
/** * @param Model $question * @param $data * @param Model $author * * @return static */ public function createAnswer(Model $question, $data, Model $author) { $answer = new static(); $answer->fill(array_merge($data, ['author_id' => $author->id, 'author_type' => get_class($author)])); $question->anwers()->save($answer); return $answer; }
public function createComment(Model $commentable, $data, Model $creator) { $comment = new static(); $comment->fill(array_merge($data, ['creator_id' => $creator->id, 'creator_type' => get_class($creator)])); $commentable->comments()->save($comment); return $comment; }
/** * Creates a new log record using Eloquent. * * @param Model $model * * @return bool */ public function createLog(Model $model) { if ($model->save() && $model->exists) { return true; } return false; }
public function getAds() { $query = $this->model->where('active', 1)->whereHas('photos', function ($q) { $q->latest()->take(1); })->take(2)->get(); return $query; }
public function __get($key) { if ($this->isEloquent()) { return $this->data->__get($key); } return object_get($this->data, $key); }
/** * Delete this customer credit card in the billing gateway. * * @return Creditcard */ public function delete() { if (!$this->model->readyForBilling()) { return $this; } $this->card->delete(); $cards = array(); foreach ($this->model->billing_cards as $c) { if (Arr::get($c, 'id') != $this->id) { $cards[] = $c; } } $this->model->billing_cards = $cards; $this->model->save(); // Refresh all subscription records that referenced this card. if ($subscriptions = $this->model->subscriptionModelsArray()) { foreach ($subscriptions as $subscription) { if ($subscription->billingIsActive() && $subscription->billing_card == $this->id) { $subscription->subscription()->refresh(); } } } $this->info = array('id' => $this->id); return $this; }
/** * However, with normal autoincrement ids - we have to set the slug after * the model has been persisted to the database - as then we have the id. * * @param $model */ public function created(Model $model) { if ($model->slugStrategy() == 'id') { $model->generateIdSlug(); $model->save(); } }
public function save(Model $model) { $model->save(); if ($model->hasUpdatedTags()) { //$model->tags()->sync($model->getUpdatedTagIds()); } }
/** * Register periods macro. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $model * @return void */ protected function registerMacro(Builder $query, Model $model) { /** * Query scope periods - filter this or last/next N periods * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $unit Period type [minute|hour|day|week|month|year] * @param integer $periods Number of past periods * @param string $column Column to match against * @param integer $includeCurrent Whether to include current period in filter (additionally) * @return \Illuminate\Database\Eloquent\Builder * * @throws \InvalidArgumentException */ $macro = function (Builder $query, $unit, $periods, $column = null, $includeCurrent = false) use($model) { if (!in_array($unit, ['minute', 'hour', 'day', 'week', 'month', 'year'])) { throw new InvalidArgumentException('Invalid period type'); } // Developer may pass $includeCurrent instead of $column if (func_num_args() == 4 && is_bool($column)) { $includeCurrent = $column; $column = null; } $column = $column ?: $model->getPeriodColumnName(); $range = $this->getPeriodRange($unit, $periods, $includeCurrent); return $query->whereBetween($column, $range); }; $query->macro('periods', $macro); }