laravel-collection Questions

7

Solved

Might not be a question specific to Eloquent collections, but it just hit me while working with them. Let's just assume we have a $collection object which is an instance of Illuminate\Support\Colle...
Nessy asked 3/9, 2013 at 12:52

13

Solved

This may be a trivial question but I am wondering if Laravel recommends a certain way to check whether an Eloquent collection returned from $result = Model::where(...)->get() is empty, as well a...
Ameba asked 13/12, 2013 at 9:35

5

Solved

I don´t want to remove the duplicate values, I want to get the articles_id duplicates and sum their quantity values, for example, this is my collection: Collection {#306 ▼ #items: array:3 [▼ 0 =...
Ochlophobia asked 30/11, 2016 at 12:17

6

Solved

Here is how I am trying to paginate: $posts = Post::all()->sortByDesc("created_at")->pagination(1); But I get this error: Method Illuminate\Database\Eloquent\Collection::paginatio...

8

Solved

I have the following query $outings = Outing::all()->groupBy(function ($item) { return Carbon::parse($item['start'])->format('m/d/Y'); }); return response()->json([ 'outings' => $ou...
Airway asked 28/6, 2018 at 15:53

5

How can i get 'natural order' in 'Eloquent ORM'? In table I have column 'text' (string). Normal order: Model::orderBy('text') 'value 1' 'value 12' 'value 23' 'value 3' 'value 8' I need this: ...
Heer asked 17/7, 2013 at 0:59

5

Solved

I want to use map or transform in paginated collection in laravel 5.5 but I am struggling it work This is what I was trying to do but getCollection is not available in LengthAwarePaginator as what...
Undesirable asked 10/9, 2019 at 9:24

3

Solved

I'm writing tests and I want to assert, that a returned collection has some specific structure. For asserting jsons I'm using assertJsonStructure() method on the Responce object. I have not fou...
Linguini asked 2/11, 2017 at 7:38

9

I have a questions table and a tags table. I want to fetch all questions from tags of a given question. So, for example, I may have the tags "Travel," "Trains" and "Culture" attached to a given que...
Chickadee asked 29/5, 2015 at 6:25

4

Solved

I have a laravel collection object. I want to use the nth model within it. How do I access it? Edit: I cannot find a suitable method in the laravel documentation. I could iterate the collection...
Hersey asked 26/6, 2014 at 9:54

9

Solved

Laravel 5.2 has pretty nice Helpers, I would like to use them to do following: I have Eloquent Model Collection: $lesson->users(); // returns Eloquent collection of 3 users pluck() function ...
Pilau asked 10/5, 2016 at 14:31

5

Solved

I guess I am breaking all the rules by deliberately making a duplicate question... The other question has an accepted answer. It obviously solved the askers problem, but it did not answer the titl...
Psoriasis asked 6/11, 2016 at 0:23

4

Solved

I have a Seller object which has a related User. I need to fill a select from LaravelCollective so I need to make something like this: {!! Form::selectGroup('seller_id', 'Seller', Seller::with('us...
Huntley asked 16/11, 2016 at 14:51

2

I have a collection returned from the database through a hasMany-relation. $items = collect([ [ 'ref' => 'efaef954', 'children' => collect([ [ 'ref' => 'wetk4', 'order' => 1, ], ...
Tombstone asked 23/3, 2021 at 14:0

6

Solved

How can i convert this Laravel collection array to json format like below. //All records from users table. $users = DB::table('users')->get(); // Required json format. return '{ "data": [ {...
Beam asked 18/9, 2017 at 7:47

2

Solved

I'm trying to combine and sort the results from several db queries. $events = collect(); $downedEvents = EventDowned::where('mission', $missionId) ->orderBy('mission_time', 'asc') ->get()...
Interglacial asked 12/3, 2017 at 22:41

2

Is there a (simple) way to convert an Eloquent Collection to an actual array without converting the elements themselves? I get in trouble when I try to pass the collection into a php method l...
Bothnia asked 8/11, 2018 at 16:16

1

Solved

Here are the dumped collections: //$collOne Illuminate\Database\Eloquent\Collection {#3386 #items: array:6 [ 4807 => "{"color_id":7,"size_id":4,"pack_id":null}" 4808 => "{"color_id":7,"si...
Blunk asked 18/10, 2018 at 18:52

3

Solved

Folks, I want to sort following nested collection by string alphabeticaly: $collection = collect([ ["name"=>"maroon"], ["name"=>"zoo"], ["name"=>"ábel"], ["name"=>"élof"] ])->so...
Donnetta asked 7/9, 2016 at 16:13

2

Solved

I have a var: $a. I don't know what it is. I want to check if I can count it. Usually, with only array, I can do this: if (is_array($a)) { echo count($a); } But some other things are countable....
Circumfluent asked 20/3, 2017 at 9:12

1

Solved

Trying to add new property to existing collection and access that. What I need is something like: $text = Text::find(1); //Text model has properties- id,title,body,timestamps $text->user = $us...
Patina asked 18/11, 2017 at 9:18

1

Solved

This is code that iterate collection in Laravel: $usersData = $users->mapWithKeys(function ($item) { return [$item->id => array("name" => $item->name, "email" => $item->email...
Topography asked 11/10, 2017 at 21:1

2

I am looping through a Laravel collection sorted by created_at to make a timeline. The start date for each item is created_at, and the end date is the created_at of the following item. If it is the...
Borchert asked 31/1, 2016 at 12:55

2

Solved

If I run $collection->filter(myFilter), Laravel does this annoying thing of adding keys to each model in the collection like so: { "4": { "myObject": "data" }, "7": { "myObject": "data" }...
Blanchette asked 12/2, 2017 at 0:33

1

Solved

I have been trying to create an array using laravel's collection function called mapWithKeys, but I couldn't achieve what I need. Here is my code, $years = range(1900, date('Y')); return collect...
Zanazander asked 1/2, 2017 at 12:37

© 2022 - 2024 — McMap. All rights reserved.