本文整理汇总了PHP中Illuminate\Database\Eloquent\Collection::find方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::find方法的具体用法?PHP Collection::find怎么用?PHP Collection::find使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Collection
的用法示例。
在下文中一共展示了Collection::find方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: updateArrangement
public static function updateArrangement(Collection $collection, array $arrangement)
{
$order = 0;
foreach ($arrangement as $parent) {
/** @var \CipeMotion\Medialibrary\Entities\Category $category */
$category = $collection->find(array_get($parent, 'id', 0));
$children = array_get($parent, 'children', []);
if (!is_null($category)) {
$category->order = $order;
$category->parent_id = null;
$category->save();
if (count($children) > 0) {
$childOrder = 0;
foreach ($children as $child) {
/** @var \CipeMotion\Medialibrary\Entities\Category $childCategory */
$childCategory = $collection->find(array_get($child, 'id', 0));
if (!is_null($childCategory)) {
$childCategory->order = $childOrder;
$childCategory->parent_id = $category->id;
$childCategory->save();
$childOrder++;
}
}
}
$order++;
}
}
}
示例2: getCounty
protected function getCounty($municipality_id)
{
$county_id = substr(str_pad($municipality_id, 4, '0', STR_PAD_LEFT), 0, 2);
/* @var County $county */
$county = $this->counties->find($county_id);
return $county;
}
示例3: __get
/**
* Retrieve token data
*
* @param string $tokenName
*
* @return mixed
*/
public function __get($tokenName)
{
$token = $this->tokens->find($tokenName);
if (!$token instanceof Token) {
return null;
}
$tokenVal = $token->token_value;
return $tokenVal;
}
示例4: sortByLikes
private function sortByLikes(Collection $collection, Collection $orderArray)
{
$sorted = new Collection();
$orderArray->each(function ($orderElem) use($collection, $sorted) {
if ($collection->contains($orderElem->likeable_id)) {
$sorted->push($collection->find($orderElem->likeable_id));
}
});
return $sorted;
}
示例5: save
/**
*
*/
public function save()
{
if (empty($this->data)) {
return;
}
if ($this->relation instanceof Relations\HasMany && is_array($this->data)) {
if (is_numeric($this->data[0])) {
$data = new Collection();
foreach ($this->data as $key => $item) {
// $this->data[$key] = $this->related->baseModel->find($item);
$data->push($this->related->baseModel->find($item));
}
} else {
$data = new Collection($this->data);
}
// detach any existing models and only save the selected ones
$foreignKey = $this->relation->getPlainForeignKey();
$current = $this->relation->getResults();
if (!$current) {
$this->relation->saveMany($data->toArray());
return;
}
$all = $data->merge($current);
foreach ($all as $item) {
if ($keep = $data->find($item->getKey())) {
$this->relation->save($keep);
} else {
$item->{$foreignKey} = null;
$item->save();
}
}
} else {
if ($this->relation instanceof Relations\BelongsToMany && is_array($this->data)) {
if (is_numeric($this->data[0])) {
$this->relation->sync($this->data);
}
} else {
/* If we have an id let's grab the model instance, otherwise assume we were given it */
$this->data = is_numeric($this->data) ? $this->related->baseModel->find($this->data) : $this->data;
parent::saveRelation($this->relation, $this->data);
}
}
}
示例6: find
/**
* {@inheritdoc}
*/
public function find($id)
{
$this->boot();
return $this->collection->find($id);
}
示例7: buildItemData
/**
* Build the pivot table data for a particular item.
*
* @param mixed $key The product's primary key.
* @param array $data
* @param \Illuminate\Database\Eloquent\Collection $products
*
* @return array
*/
protected function buildItemData($key, array $data, Collection $products)
{
if (!($product = $products->find($key))) {
throw new \RuntimeException("ID {$key} not found in products collection");
}
$this->setProduct($product);
if (!empty($data['purchased_amount'])) {
$data['purchased_for'] = $product->in_price;
}
if (!empty($data['sold_amount'])) {
$data['sold_for'] = $product->out_price;
}
return $data;
}
示例8: updateUserHashtags
/**
* Updates the current user's searched hashtags with the specified hashtag
* $terms and returns the number of hashtags updated or -1 on failure.
*
* @param Collection $hashtags hashtag collection to update
* @param array|string $terms hashtag terms to update with
* @return integer
*/
protected function updateUserHashtags(Collection $hashtags, array $terms)
{
$num_updates = 0;
foreach ($terms as $id => $term) {
// remove whitespace, convert to lowercase
$term = strtolower(preg_replace('/\\s+/', '', $term));
if (!empty(trim($term))) {
// verify hashtag term does not already exists
$hashtag = $hashtags->first(function ($key, $value) use($id, $term) {
return $value->id != $id && $value->term == $term;
});
if (isset($hashtag)) {
// indicate error has occured
$num_updates = -1;
\Session::put('error_hashtag_edit_id', $id);
\Session::flash('flash_message', 'Hashtag \'' . $term . '\' already exists.');
break;
}
// update hashtag term if not the same
$hashtag = $hashtags->find($id);
if (isset($hashtag) && $hashtag->term != $term) {
$hashtag->term = $term;
$hashtag->save();
$num_updates++;
}
}
}
return $num_updates;
}