本文整理汇总了PHP中Illuminate\Database\Eloquent\Model类的典型用法代码示例。如果您正苦于以下问题:PHP Model类的具体用法?PHP Model怎么用?PHP Model使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Model类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createQuestion
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;
}
示例2: deleteThe
/**
* 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();
}
示例3: save
/**
* 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);
}
示例4: __construct
/**
* 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);
}
示例5: update
/**
* 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);
}
示例6: createReview
/**
* @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;
}
示例7: attachRelationship
/**
* 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;
}
示例8: import
/**
* 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();
}
}
示例9: aggregate
/**
* 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;
}
示例10: mergeValues
/**
* 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());
}
}
示例11: storeVehicle
/**
* 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;
}
示例12: createRating
/**
* @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;
}
示例13: attachTags
/**
* @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);
}
}
示例14: authorizeForResults
/**
* @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);
}
示例15: destroy
/**
* 删除资源
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return mixed
* @throws \Exception
*/
public function destroy(Model $model)
{
if ($model->delete()) {
return $this->success('删除成功');
}
return $this->error('删除失败');
}