public function calculate(Collection $results) { $allDelta = $results->lists('delta')->toArray(); $allLambda = $results->lists('lambda')->toArray(); $slope = $this->calculateSlope($allDelta, $allLambda); $yIntercept = $this->calculateYIntercept($allDelta, $allLambda, $slope); return new RegressionLine($slope, $yIntercept); }
/** * Sync a user with new organisations. * * @param \App\Models\User\User $user * @param \Illuminate\Support\Collection $organisations * * @return void */ public function sync(User $user, Collection $organisations) { $existing = Organisation::query()->whereIn('id', $organisations->lists('id'))->get(); $user->organisations()->detach(); $organisations = $this->map($organisations); $organisations = $this->create($organisations, $existing); $user->organisations()->attach($organisations->lists('id')->toArray()); }
public function getForumThreadsByTagsPaginated(Collection $tags, $perPage = 20) { $query = $this->model->with(['slug', 'mostRecentChild', 'tags'])->where('type', '=', COMMENT::TYPE_FORUM)->join('comment_tag', 'comments.id', '=', 'comment_tag.comment_id'); if ($tags->count() > 0) { $query->whereIn('comment_tag.tag_id', $tags->lists('id')); } $query->groupBy('comments.id')->orderBy('updated_at', 'desc'); return $query->paginate($perPage, ['comments.*']); }
/** * Assign permissions to roles * * @param Collection $permissions */ private function assignPermissions(Collection $permissions) { $roles = Role::whereIn('id', $this->role_ids)->get(); if ($roles && $permissions) { $permission_ids = $permissions->lists('id')->all(); foreach ($roles as $role) { $role->permissions()->attach($permission_ids); } } }
static function setFavourites(Collection $products, User $user) { $favourites = $user->favourites()->whereIn('product_id', $products->lists('id'))->withPivot('created_at')->get(['product_id as pid', 'favourites.created_at as added_fav']); foreach ($favourites as $key => $fav) { foreach ($products as $item) { if ($item->id == $fav->pid) { $item->favourite = $fav->added_fav; } } } }
/** * Save the file to CSV. * * @param $path * @param string $delimiters * @param string $wrapper * @param string $prefix * * @return mixed */ public function saveToCSV($path, $delimiters = ';', $wrapper = '"', $prefix = 'backup') { $response = new Collection(); $this->delimiters = $delimiters; $this->wrapper = $wrapper; $this->createFileDirectoryIfNeeded($path); foreach ($this->tablesContent as $tableName => $tableContent) { // If table is empty, continue to the next if (!count($tableContent)) { continue; } $fileName = $path . date('Y-m-d-H-i-s') . "-{$prefix}-{$tableName}.csv"; $response->push(['status' => $this->writeCSVFile($fileName, $tableContent), 'tableName' => $tableName, 'filename' => $fileName]); } // If there was at least one error if (in_array(false, $response->lists('status')->all())) { return Response::make(['message' => 'These was an error !', 'status' => 400, 'data' => $response], 400); } $this->dispatcher->fire(new DatabaseTableWasSavedToCSV($response->lists('tableName')->all())); return Response::make(['message' => 'Saved !', 'status' => 200, 'data' => $response], 200); }
public function index() { $posts = Blog::all(); $post_collection = new Collection($posts); $blog_id_list = $post_collection->lists('id'); $id_arr = array(); foreach ($blog_id_list as $id) { array_push($id_arr, $id); } $rand_id = mt_rand(0, count($id_arr) - 1); $banner_post = Blog::findOrFail($id_arr[$rand_id]); //($banner_post->toArray()); return view('blog.homepage', compact('posts'))->with(['banner_post' => $banner_post]); }
public function getByTagsAndStatusPaginated(Collection $tags, $status, $perPage = 20) { $query = $this->model->with(['author', 'mostRecentReply', 'acceptedSolution']); if ($tags->count() > 0) { $query->join('tagged_items', 'forum_threads.id', '=', 'tagged_items.thread_id')->whereIn('tagged_items.tag_id', $tags->lists('id')); $query->groupBy('forum_threads.id'); } if ($status) { if ($status == 'solved') { $query->where('solution_reply_id', '>', 0); } if ($status == 'open') { $query->whereNull('solution_reply_id'); } } $query->orderBy('updated_at', 'desc'); return $query->paginate($perPage, ['forum_threads.*']); }
/** * Foreach role in the given roles: * 1. If it doesn't exist in the database then create it * * Foreach role in the database roles: * 1. If it doesn't exist in the given roles then delete it * * @param Collection $roles * @return int */ public static function matchRoles(Collection $roles) { $dbRoles = static::all(); $types = $roles->lists('type'); $dbTypes = $dbRoles->lists('type'); foreach ($roles as $role) { // If it doesn't exist in the database if (!in_array($role->type, $dbTypes)) { // then create it $role->save(); } } foreach ($dbRoles as $dbRole) { // If database role doesn't exist in the given roles then delete it if (!in_array($dbRole->type, $types)) { $dbRole->delete(); } } }
/** * Gets events which are associated with a list of tags. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Support\Collection $tags * @return \Illuminate\Database\Eloquent\Builder */ public function scopeWhereHasTags($query, Collection $tags) { return $query->whereHas('categories', function ($constraint) use($tags) { $constraint->whereIn('tags.id', $tags->lists('id')); }); }
/** * Convert a collection for use in a dropdown. * * @param Collection $collection * @param string $key * @param string $value * @return array */ public function forDropdown(Collection $collection, $key, $value) { if ($collection->count() > 0) { return $collection->lists($value, $key); } return $collection->toArray(); }
/** * Get an array with the values of a given column. * * @param string $column * @param string $key * @return array */ public function lists($column, $key = null) { $columns = $this->getListSelect($column, $key); $results = new Collection($this->get($columns)); return $results->lists($columns[0], array_get($columns, 1)); }
/** * Display all fields and their metadata, visibility option, and new metadata form * * @params int $pid, int $fid * @return Response */ public function index($pid, $fid) { //Fields that already have metadata do not get sent to the view to be listed $all_fields = Field::where('pid', $pid)->where('fid', $fid)->get(); $available_fields = new \Illuminate\Support\Collection(); foreach ($all_fields as $field) { if ($field->metadata()->first() !== null) { continue; } else { $available_fields->push($field); } } $fields = $available_fields->lists('name', 'flid'); $form = Form::find($fid); return view('metadata.index', compact('pid', 'fid', 'form', 'fields')); }
/** * Load permissions of user if not exist * * @return void */ protected function loadPermissions() { if (is_null($this->slugPermissions)) { if ($this->isLoadRoles()) { if (!$this->isLoadPermissions()) { $this->roles->load('permissions'); } } else { $this->load(['roles.permissions']); } $slugPermissions = new Collection(); $permissions = new Collection(); $this->roles->each(function ($item, $key) use(&$permissions, &$slugPermissions) { $slugPermissions = $slugPermissions->merge($item->permissions->lists('slug')); $item->permissions->each(function ($v, $k) use(&$permissions) { $tmpSlug = $permissions->lists('slug'); if ($tmpSlug->search($v->slug) === false) { $permissions->push($v); } }); }); $this->permissions = $permissions; $this->slugPermissions = $slugPermissions->unique(); } }
private function getProperties($data) { $data = new Collection($data); $articleNumbers = $data->lists('Artnr'); $properties = DB::connection('askas')->table('Artiklar_Egenskaper')->leftJoin('Artiklar_Egenskaper_Falt', 'Artiklar_Egenskaper.ArtFalt_ID', '=', 'Artiklar_Egenskaper_Falt.ArtFalt_ID')->whereIn('Artiklar_Egenskaper.Artnr', $articleNumbers)->where('Sprak_Suffix', 'SV')->whereNotNull('Namn_SV')->select(['Artiklar_Egenskaper.Artnr', 'Artiklar_Egenskaper.Varde', 'Artiklar_Egenskaper_Falt.Namn_SV'])->get(); $properties = new Collection($properties); //need to make it an array again. The edit in foreach won't work otherwise $data = $data->toArray(); foreach ($data as &$article) { $article = (array) $article; $article['properties'] = $properties->where('Artnr', $article['Artnr'])->all(); $article['recommendedPrice'] = $this->getProperty($properties, "Rek. pris", $article["Artnr"]); $article['supplierArticleNumber'] = $this->getProperty($properties, "Leverantörs artikelnummer", $article["Artnr"]); $article['series'] = $this->getProperty($properties, "SERIE", $article["Artnr"]); $article['color'] = $this->getProperty($properties, 'Färg', $article["Artnr"]); } return $data; }
/** * Get the top browsers for the given period. * * @param DateTime $startDate * @param DateTime $endDate * @param int $maxResults * * @return Collection */ public function getTopBrowsersForPeriod(DateTime $startDate, DateTime $endDate, $maxResults) { $browserData = []; $answer = $this->performQuery($startDate, $endDate, 'ga:sessions', ['dimensions' => 'ga:browser', 'sort' => '-ga:sessions']); if (is_null($answer->rows)) { return new Collection([]); } foreach ($answer->rows as $browserRow) { $browserData[] = ['browser' => $browserRow[0], 'sessions' => $browserRow[1]]; } $browserCollection = new Collection(array_slice($browserData, 0, $maxResults - 1)); if (count($browserData) > $maxResults) { $otherBrowsers = new Collection(array_slice($browserData, $maxResults - 1)); $otherBrowsersCount = array_sum(Collection::make($otherBrowsers->lists('sessions'))->toArray()); $browserCollection->put(null, ['browser' => 'other', 'sessions' => $otherBrowsersCount]); } return $browserCollection; }
/** * @param Builder $builder * @param Collection $pages */ public function scopeBut(Builder $builder, Collection $pages) { if ($pages->count() > 0) { $builder->whereNotIn($this->getKeyName(), $pages->lists($this->getKeyName())->toArray()); } }
/** * @param array $newMediaArray * @param $collectionName * * @return mixed */ private function removeMediaItemsNotPresentInArray(array $newMediaArray, $collectionName) { $newMediaItems = new Collection($newMediaArray); foreach ($this->getMedia($collectionName, []) as $currentMedia) { if (!in_array($currentMedia->id, Collection::make($newMediaItems->lists('id'))->toArray())) { $this->removeMedia($currentMedia->id); } } }
/** * @param \Illuminate\Support\Collection $files * @return \Illuminate\Support\Collection */ private function getFilenames($files) { return $files->lists('path'); }