本文整理汇总了PHP中Illuminate\Database\Eloquent\Collection类的典型用法代码示例。如果您正苦于以下问题:PHP Collection类的具体用法?PHP Collection怎么用?PHP Collection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Collection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getMaps
/**
*
* $newsletter
*/
protected function getMaps()
{
$posts = new Collection();
$projects = new Collection();
$this->each(function ($widget) use($projects, $posts) {
if ($widget->resource) {
if ($widget->resource_type == Post::class) {
$posts->push($widget->resource_id);
}
if ($widget->resource_type == Project::class) {
$projects->push($widget->resource_id);
}
}
if ($widget->other_resource) {
if ($widget->other_resource_type == Post::class) {
$posts->push($widget->other_resource_id);
}
if ($widget->other_resource_type == Project::class) {
$projects->push($widget->other_resource_id);
}
}
});
$posts = Post::whereIn('posts.id', $posts->all())->with(['translations', 'images', 'images.sizes'])->get();
$projects = Project::whereIn('portfolio_projects.id', $projects->all())->with(['translations', 'images', 'images.sizes'])->get();
return [$posts, $projects];
}
示例2: create
public function create(Request $request, $student_id = null)
{
$message_types = $this->message_type->orderBy('message_type_name')->lists('message_type_name', 'id')->all();
$schools = $this->school->lists('school_name', 'id')->all();
if (isset($student_id)) {
$student = $this->contact->find($student_id);
} else {
$student = null;
}
$all_contacts = $this->contact->allStudents();
$contacts = array();
foreach ($all_contacts as $contact) {
$contacts[$contact->id] = $contact->name . ' ' . $contact->surname;
}
if ($request->input('message_id')) {
$messages = Message::processIds($request->input('message_id'));
} else {
$selected = null;
}
$students = new Collection();
$selected = array();
if (isset($messages)) {
foreach ($messages as $message) {
$students->add($message->Contact);
$selected[] = $message->Contact->id;
}
$students = $students->unique();
}
return view()->make('messages.create', compact('message_types', 'schools', 'contacts', 'student', 'selected'));
}
示例3: render
/**
* Render the likes into a string.
*
* @param Collection $likesCollection The likes to render.
*
* @param string $viewAllLikesLink The link to view all of the likes for the content.
*
* @return string
*/
public function render(Collection $likesCollection, $viewAllLikesLink)
{
$numLikesToList = $this->settings->get('posts.likes_to_show', 3);
$numOtherLikes = $likesCollection->count() - $numLikesToList;
$userId = $this->guard->user()->getAuthIdentifier();
$likes = [];
$likesCollection = $likesCollection->filter(function (Like $like) use(&$likes, &$numLikesToList, $userId) {
if ($like->user->id === $userId) {
$like->user->name = $this->lang->get('likes.current_user');
$likes[] = $like;
$numLikesToList--;
return false;
}
return true;
});
$numLikesInCollection = $likesCollection->count();
if ($numLikesInCollection > 0 && $numLikesToList > 0) {
if ($numLikesInCollection < $numLikesToList) {
$numLikesToList = $numLikesInCollection;
}
$randomLikes = $likesCollection->random($numLikesToList);
if (!is_array($randomLikes)) {
// random returns a single model if $numLikesToList is 1...
$randomLikes = array($randomLikes);
}
foreach ($randomLikes as $key => $like) {
$likes[] = $like;
}
}
return $this->viewFactory->make('likes.list', compact('numOtherLikes', 'likes', 'viewAllLikesLink'))->render();
}
示例4: update_groupe_post
public function update_groupe_post(Request $request, $id)
{
$user = Auth::user();
if ($user->profil->intitule == "administrateur") {
$erreurs = new Collection();
$this->validate($request, ['intitule' => 'required', 'description' => 'required', 'parcours' => 'exists:parcours,id']);
$groupes = Groupe::all();
$groupe = Groupe::find($id);
foreach ($groupes as $g) {
if ($request->input('intitule') != $groupe->intitule) {
if ($request->input('intitule') == $g->intitule) {
$erreurs->prepend("Cet intitulé existe déjà !");
break;
}
}
}
$groupe->intitule = $request->input('intitule');
$groupe->description = $request->input('description');
$groupe->parcours_id = $request->input('parcours');
$parcours = Parcours::all();
if (count($erreurs) > 0) {
return response()->view('groupe/update_groupe', ['groupe' => $groupe, 'erreurs' => $erreurs, 'parcours' => $parcours]);
}
$groupe->save();
return redirect('admin/groupe');
}
return "Vous êtes pas administrateur";
}
示例5: collection
public function collection(Collection $collection)
{
$this->data = $collection->map(function ($item) {
return $this->makeData($item);
})->all();
return $this;
}
示例6: update_profil_post
public function update_profil_post(Request $request, $id)
{
$user = Auth::user();
if ($user->profil->intitule == "administrateur") {
$erreurs = new Collection();
$this->validate($request, ['intitule' => 'required']);
$profils = Profil::all();
$profil = Profil::find($id);
foreach ($profils as $p) {
if ($request->input('intitule') != $profil->intitule) {
if ($request->input('intitule') == $p->intitule) {
$erreurs->prepend("Cet intitulé existe déjà !");
break;
}
}
}
$profil->intitule = $request->input('intitule');
if (count($erreurs) > 0) {
return response()->view('profil/update_profil', ['profil' => $profil, 'erreurs' => $erreurs]);
}
$profil->save();
return redirect('admin/profil');
}
return "Vous êtes pas administrateur";
}
示例7: addTrainingData
public function addTrainingData($user, $training)
{
$collection = new Collection();
$visible = true;
foreach ($training as $itemKey => $item) {
$item['visible'] = $visible;
foreach ($item['relations'] as $relationKey => $relation) {
$done = true;
foreach ($relation as $elementKey => $element) {
$element['id'] = "{$item['id']}.{$element['id']}";
$element['watch-url'] = route('training.watch', ['year' => 2016, 'item' => $element['id']]);
$element['watched'] = Watched::where('subscription_id', $user->id)->where('item_id', $element['id'])->first();
$element['visible'] = $visible || $element['watched'];
$done = $done && $element['watched'];
if ($relationKey == 'quiz') {
$element['answer'] = $element['watched'] ? $element['watched']->answer : null;
}
$visible = $element['watched'] !== null;
$item['relations'][$relationKey][$elementKey] = $element;
}
}
$item['done'] = $done;
$collection->push($item);
}
return $collection;
}
示例8: getRawCollectionData
private function getRawCollectionData(Collection $collection)
{
foreach ($collection as $element) {
$this->execute_with_relations ? $element->loadDisplayableRelations() : null;
}
return $collection->toArray();
}
示例9: upload
public function upload(PhotoUploadRequest $request)
{
$user = Auth::user();
$destinationPath = 'uploads/';
if ($request->hasFile('files')) {
$photos = new Collection();
$i = 0;
foreach ($request->file('files') as $file) {
$extension = $file->getClientOriginalExtension();
$new_filename = time() . uniqid() . str_random(rand(5, 15)) . '.' . $extension;
$file->move($destinationPath, $new_filename);
$photos[$i] = new Photo();
$photos[$i]->content_type = $file->getClientMimeType();
$photos[$i]->disk_name = $new_filename;
$photos[$i]->file_name = $file->getClientOriginalName();
$photos[$i]->path = $destinationPath . $new_filename;
$photos[$i]->file_size = $file->getClientSize();
$photos[$i]->user_id = $user->id;
$photos[$i]->save();
$i++;
}
return $photos->first();
}
return response()->json(['status' => 'error']);
}
示例10: transformCollection
/**
* @param $items
* @return array
*/
public function transformCollection(Collection $items, $showHref = false)
{
$data = $items->map(function ($item) use($showHref) {
return $this->transform($item, $showHref);
})->toArray();
return $data;
}
示例11: createData
/**
* @param Collection $model
* @param string $single
*
* @return string
*/
public function createData($model, string $single)
{
$str = sprintf("\n\t\t\$scope.%s = new Minute.Models.%sArray(null);\n", $single, $this->fixName($single));
$str .= sprintf("\t\t\$scope.%s.load(%s);\n", $single, json_encode($model->toArray(), JSON_PRETTY_PRINT));
//{metadata: {offset: 0, limit: 2, total: %d}, items: %s}
return $str;
}
示例12: handle
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$month = new Carbon($this->argument('month'));
$billboards = Billboard::activated()->get();
$pictures = Storage::files();
$missing_pictures = new Collection();
foreach ($billboards as $billboard) {
$facings = $billboard->facings->all();
foreach ($facings as $facing) {
$ref = $this->reference($billboard, $facing, $month);
$image_url = "{$ref}.jpg";
if (in_array($image_url, $pictures)) {
try {
$this->trackingRepository->create(['facing_id' => $facing->id, 'image_url' => $image_url, 'tracking_date' => $month, 'location_id' => $billboard->location_id]);
$this->info('Tracking #' . $ref . ' with image url ' . $image_url . ' created successfully.');
} catch (\Illuminate\Database\QueryException $e) {
$this->error('Tracking #' . $ref . ' already exists.');
}
} else {
$this->error('Facing #' . $ref . ' picture missing.');
$missing_pictures->push(["name" => $facing->name, "image_url" => $image_url]);
}
}
}
if (!$missing_pictures->isEmpty()) {
$this->notifyMissingPictures($missing_pictures, "it@sagaciresearch.com");
}
}
示例13: index
public function index()
{
$sections = Section::all();
if (Auth::user()) {
$cart = Auth::user()->cart;
} else {
$cart = new Collection();
if (Session::has('cart')) {
foreach (Session::get('cart') as $item) {
$elem = new Cart();
$elem->product_id = $item['product_id'];
$elem->amount = $item['qty'];
if (isset($item['options'])) {
$elem->options = $item['options'];
}
$cart->add($elem);
}
}
}
$total = 0;
$options = new Collection();
foreach ($cart as $item) {
$total += $item->product->price * $item->amount;
if ($item->options) {
$values = explode(',', $item->options);
foreach ($values as $value) {
$options->add(OptionValue::find($value));
}
}
}
return view('site.cart', compact('sections', 'total', 'cart', 'options'));
}
示例14: 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++;
}
}
}
示例15: show
/**
* Display the specified resource.
*
* @param int|null $id
*
* @return \Illuminate\Http\Response
*/
public function show($id = null)
{
if ($id === null) {
$profile = Auth::user()->getProfile();
} else {
$profile = Profile::findOrFail($id);
}
$is_mine = $profile->user_id == \Flocc\Auth::getUserId();
if ($is_mine === true) {
$activities = (new Activities())->get();
$tribes = (new Tribes())->get();
$events_time_lines = new Collection();
foreach ($profile->getTimeLine()->getLatestUpdatedEvents() as $event) {
foreach ($event->getTimeLine() as $line) {
if ($line->isMessage()) {
$events_time_lines->push(['id' => $event->getId(), 'slug' => $event->getSlug(), 'event' => $event->getTitle(), 'date' => $line->getTime(), 'message' => $line->getMessage()]);
}
}
}
$events_time_lines = $events_time_lines->sortByDesc('date')->slice(0, 5);
return view('dashboard', compact('profile', 'is_mine', 'activities', 'tribes', 'events_time_lines'));
} else {
return view('profiles.show', compact('profile', 'is_mine', 'id'));
}
}