本文整理汇总了PHP中Illuminate\Database\Eloquent\Collection::filter方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::filter方法的具体用法?PHP Collection::filter怎么用?PHP Collection::filter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Collection
的用法示例。
在下文中一共展示了Collection::filter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: map
public function map($hash, $title = true)
{
if (strlen($hash) < 1) {
$hash = '9999999999';
}
if (Hashes::$items == null) {
$this->getItems();
}
$object = Hashes::$items->filter(function ($item) use($hash) {
return $item->hash == $hash;
})->first();
if ($object instanceof Hash) {
if ($title) {
return $object->title;
}
return $object;
} else {
if ($this->allowedRetry) {
$this->updateHashes();
return $this->map($hash, $title);
} else {
$classified = Hash::where('hash', '9999999999')->first();
if ($title) {
return $classified->title;
}
return $classified;
// throw new HashNotLocatedException($hash);
}
}
}
示例2: handle
/**
* Execute the job.
*
* @return Collection
*/
public function handle()
{
return $this->bookings->filter(function ($booking) {
$value = 0;
$payed = 0;
foreach ($booking->customer_booking as $customer) {
if ($customer->price) {
$value += $customer->price->price;
}
}
foreach ($booking->payment as $payment) {
$payed += $payment->value;
}
if ($value === $payed) {
return true;
}
return false;
})->sortByDesc('id');
}
示例3: isEnabledKey
/**
* Return true if all API Key are still enable
*
* @param Collection $keys
* @return bool
*/
protected function isEnabledKey(Collection $keys)
{
// count keys with enable value and compare it to total keys number
$enabledKeys = $keys->filter(function ($item) {
return $item->enabled == 1;
})->count();
if ($enabledKeys == $keys->count() && $keys->count() != 0) {
return true;
}
return false;
}
示例4: match
public function match(array $models, Collection $results, $relation)
{
$match = $this->matchPHP;
foreach ($models as $model) {
$value = $results->filter(function ($result) use($match, $model) {
return $match($model, $result);
});
if ($value->count()) {
$model->setRelation($relation, $value);
}
}
return $models;
}
示例5: getRoles
/**
* Get all roles as collection.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getRoles()
{
if (!$this->roles) {
$this->roles = $this->grantedRoles()->get();
$deniedRoles = $this->deniedRoles()->get();
foreach ($deniedRoles as $role) {
$deniedRoles = $deniedRoles->merge($role->descendants());
}
foreach ($this->roles as $role) {
if (!$deniedRoles->contains($role)) {
$this->roles = $this->roles->merge($role->descendants());
}
}
$this->roles = $this->roles->filter(function ($role) use($deniedRoles) {
return !$deniedRoles->contains($role);
});
}
return $this->roles;
}
示例6: match
/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param \Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @return array
*/
public function match(array $models, Collection $results, $relation)
{
// We will need the parent node placeholder so that we use it to extract related results.
$parent = $this->query->getQuery()->modelAsNode($this->parent->getTable());
/**
* Looping into all the parents to match back onto their children using
* the primary key to map them onto the correct instances, every single
* result will be having both instances at each Collection item, held by their
* node placeholder.
*/
foreach ($models as $model) {
$matched = $results->filter(function ($result) use($parent, $model) {
if ($result[$parent] instanceof Model) {
// In the case of fetching nested relations, we will get an array
// with the first key being the model we need, and the other being
// the related model so we'll just take the first model out of the array.
if (is_array($model)) {
$model = reset($model);
}
return $model->getKey() == $result[$parent]->getKey();
}
});
// Now that we have the matched parents we know where to add the relations.
// Sometimes we have more than a match so we gotta catch them all!
foreach ($matched as $match) {
// In the case of fetching nested relations, we will get an array
// with the first key being the model we need, and the other being
// the related model so we'll just take the first model out of the array.
if (is_array($model)) {
$model = reset($model);
}
$model->setRelation($relation, $match[$relation]);
}
}
return $models;
}
示例7: getRecordForFile
/**
* Search through the record list for one matching the provided path.
* @param string $path
* @param \Illuminate\Database\Eloquent\Collection $existing_media
* @return \Plank\Mediable\Media|null
*/
protected function getRecordForFile($path, $existing_media)
{
$directory = File::cleanDirname($path);
$filename = pathinfo($path, PATHINFO_FILENAME);
$extension = pathinfo($path, PATHINFO_EXTENSION);
return $existing_media->filter(function (Media $media) use($directory, $filename, $extension) {
return $media->directory == $directory && $media->filename == $filename && $media->extension == $extension;
})->first();
}
示例8: handle
/**
* Execute the job.
*
* @return Collection
*/
public function handle()
{
return $this->bookings->filter(function ($booking) {
return !$booking->payment->count() ? true : false;
})->sortByDesc('id');
}
示例9: matchOneOrMany
/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param \Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @return array
*/
public function matchOneOrMany(array $models, Collection $results, $relation, $type)
{
// We will need the parent node placeholder so that we use it to extract related results.
$parent = $this->query->getQuery()->modelAsNode($this->parent->getTable());
/**
* Looping into all the parents to match back onto their children using
* the primary key to map them onto the correct instances, every single
* result will be having both instances at each Collection item, held by their
* node placeholder.
*/
foreach ($models as $model) {
$matched = $results->filter(function ($result) use($parent, $relation, $model) {
if ($result[$parent] instanceof Model) {
return $model->getKey() == $result[$parent]->getKey();
}
});
// Now that we have the matched parents we know where to add the relations.
// Sometimes we have more than a match so we gotta catch them all!
foreach ($matched as $match) {
if ($type == 'many') {
$collection = $model->getRelation($relation);
$collection->push($match[$relation]);
$model->setRelation($relation, $collection);
} else {
$model->setRelation($relation, $match[$relation]);
}
}
}
return $models;
}
示例10: getByAttributeFromCollection
/**
* Get items from collection whose properties match a given attribute and value
*
* @param Collection $collection
* @param string $attribute
* @param mixed $value
*
* @return Collection
*/
protected function getByAttributeFromCollection(Collection $collection, $attribute, $value = null)
{
return $collection->filter(function ($item) use($attribute, $value) {
if (isset($item->{$attribute}) && $value) {
return $item->{$attribute} == $value;
}
return false;
});
}
示例11: filterUnviewableForums
/**
* Filters a forum collection by the "canView" permission
*
* @param Collection $forums
*
* @return Collection
*/
private function filterUnviewableForums(Collection $forums)
{
return $forums->filter(function (Forum $forum) {
return $this->permissionChecker->hasPermission('forum', $forum->getContentId(), $forum::getViewablePermission(), $this->guard->user());
});
}
示例12: transformPost
public function transformPost(\Post $post, Collection $favorites, Collection $subscriptions)
{
$response = ['id' => $post->id, 'text' => $post->text, 'created_at' => $post->created_at, 'updated_at' => $post->updated_at, 'user' => $this->transformUserToSmall($post->user), 'attachments' => $this->transformAttachments($post), 'comments_count' => $post->comments->count(), 'likes' => $post->likes->count(), 'commented' => $post->commented(), 'liked' => $post->liked() ? 1 : 0, 'is_fav' => $favorites->filter(function ($favorite) use($post) {
return $favorite->id == $post->id;
})->count() == 1 ? 1 : 0, 'category' => ['id' => $post->category->id, 'title' => $post->category->title, 'subscribed' => $subscriptions->filter(function ($subscription) use($post) {
return $subscription->id == $post->category->id;
})->count() == 1 ? 1 : 0]];
// if ($comments)
// $response['comments'] = $this->transformComments($post->comments);
return $response;
}
示例13: siblingOfRoot
/**
* Check if node is sibling for roots in collection
*
* @param Tree $node Tree node
* @param Collection $roots Collection of roots
*
* @return bool
*/
private function siblingOfRoot(Tree $node, Collection $roots)
{
return (bool) $roots->filter(function ($item) use($node) {
return $node->isSibling($item);
})->first();
}
示例14: returnProjectModels
public function returnProjectModels(Collection $collection)
{
$user = \Authorization::user();
$wishlist = WishModel::where('user_id', '=', $user->id)->get();
if ($user->userable_type == BuyerModel::class) {
$collection->filter(function ($item) use($wishlist) {
$wishlist->contains('project_id', $item->id) ? $item->wishlisted = true : ($item->wishlisted = false);
return $item;
});
}
return $this->withCollection($collection, new ProjectTransformer());
}
示例15: getFormingGuilds
/**
* Retrieve all the forming guilds.
*
* @param \Illuminate\Database\Eloquent\Collection $guilds
* @return array
*/
protected function getFormingGuilds(Collection $guilds)
{
return $guilds->filter(function ($guild) {
return $guild->isForming();
});
}