Am I able to unset whole collection element inside transform? $jsonArrLive->transform(function ($i,$k) { $test = $i[0]->some ?? null; if(!$test) { return null; } } Now i get [‘arr1=>..’,null] and I want to remove this null inside transform to get just [‘arr1=>..’] as result I added: $jsonArrLive = $jsonArrLive->filter(function ($value, $key) { return $value != null; ..
Category : collections
In laravel I faced that issue when using array merge in controller . I have to use it because I want to show data from both arrays and show them in one table of laravel so how could I resolve that error and what does it actually means? my controller $array1=DB::table(‘table1’) ->whereDate(‘created_at’, Carbon::today()) ->select(‘table1.name as ..
in my project, I’m exporting a sorted Collection to Excel, using the Maatwebsite/Excel functionality. It is working fine for the moment. The result is a collection of cars with time worked on a car by means of different tasks. A car can have 1+ tasks/times. <?php namespace AppExports; use AppTime; use CarbonCarbon; use MaatwebsiteExcelConcernsFromCollection; use ..
I have this fulltreeCollection 0: 2 1: 10 2: 11 3: 12 subtree 0: 2 1: 3 2: 4 whatever exist in subtree but not exist in fulltreeCollection, i want to remove it. so the end result of subtree should removed 3 and 4, left with 0: 2 $fulltreeCollection = collect($fulltreetest); $subtree = collect($subtreetest); $filter2= ..
What is the equivalent of continue while looping in transform()? I want to do something like this: $collection->transform(function($item) { if ($condition) { continue; } return $item[‘key’]; }); One option will be to do a ->reject() after transform is completed but is there a way to do it while still looping? P.S: The goal is to ..

I call an API and put the response into a collection (thought it was a good idea) to reder it like a eloquent collection with a foreach in the blade My trait: class AzureRest { public static function getSubscription($subscriptionId) { $token = TokenCache::getRestApiToken(); $url = "https://management.azure.com/subscriptions/".$subscriptionId."?api-version=2020-01-01"; $json = Http::withToken($token)->get($url); return collect(json_decode($json, true, 5))->all(); } } ..
I am trying to do a multiple sortBy on a collection and seems like is not working properly: $myCollection = collect([ [‘foo’ => 3, ‘bar’ => null, ‘active’ => 1], [‘foo’ => 2, ‘bar’ => null, ‘active’ => 1], [‘foo’ => 1, ‘bar’ => 1, ‘active’ => 1], ])->sortBy(‘foo’)->sortBy(‘bar’)->sortBy(‘active’); Result: IlluminateSupportCollection {#417 ▼ #items: array:3 ..
When I’m using map() in DB query. I’m getting this result { "success": true, "message": "Purchase Order Generated", "data": [ { "po_date": "2015-05-24 00:00:00", "items": { "item_name": "Tata Steel", "specifications": "steels", "unit": "ton", "quantity": "100", "price": "1200", "amount": "120000" } }, { "po_date": "2015-05-24 00:00:00", "items": { "item_name": "Tata Steel 2", "specifications": "steels 1", "unit": ..
{ "success": true, "data": { "items": [ [], [], { "id": 1, "name": "kathmandu, Nepal", "photo": null, "description": "kathmandu, Nepal", "address": "kathmandu, Nepal", "latitude": 27.674665, "longitude": 85.405183, "isclosed": 0, "time_from": "closed", "time_to": "closed" } ], "pagination": [] }, "message": "Fetched Sucessfully" } Sourc..

I’m using external API for my website. When user select city and town, API returns neighborhoods. But the problem is, if there is only one neighborhood array contains name and neighborhood id. But if there is more than one, it contains multiple array for each neighborhood. Single example ; array( ‘Code’ => 123, ‘NeighborhoodName’ => ..
I have single query then I loop using forEach. I filter by eliminated null value then pushed into a collection And then I flattened to convert 2D array ti 1D array. So, I have data in collection format. My question is how to get MIN MAX from collection? For example my data: IlluminateSupportCollection {#193 #items: ..
Here my code : $course = Course::where(‘id’, $activeCourseId) ->with([‘cards’ => function($q) use ($cardsDueIds) { $q->whereNotIn(‘cards.id’, $cardsDueIds); $q->take(50); }, "cards.block", "cards.contexts", "cards.user_contexts", "cards.block.course", "cards.thisUser"]) ->first(); $blocks = $course->blocks; foreach ($blocks as $block) { $block[‘cards’] = []; foreach ($course[‘cards’] as $card) { if($card[‘block_id’] === $block[‘id’]) { array_push($block[‘cards’], $card); } } } I get this error : "Indirect ..
There are two tables, a table with streets, and a table with information about the house. I tried to join the tables using the street_id column in the houses table. The table works, I get the information. But now how can I make it so that when I click on the street / {ID} link, ..
I have Collection A like this… [ "Gender", "Name", "Role", ], … and I have a Collection B from a model instance like this… [ AppModelsStaff { id: 11, name: "John Doe", gender: ‘Male’, 2: "role", role: AppModelsRole { id: 1, name: "Director", }, }, AppModelsStaff { id: 22, name: "Jane Doe", gender: ‘Female’, 2: ..
I’m loading a product eagerly with its relationship data in Laravel. $product = Product::with(‘attributeValues.attribute’)->find($id)->get(); Currently I get the response structure as follows. { "product_id": 1, "product_name": "Shirt A", "attribute_values": [ { "attribute_value_id": 1, "attribute_value": "small", "attribute": { "attribute_id": 1, "attribute": "size" } }, { "attribute_value_id": 1, "attribute_value": "medium", "attribute": { "attribute_id": 1, "attribute": "size" } ..
I’m trying to to figure out how to map a static value from the database to act like and be used as a variable. So I have this query $get_header_keys = DB::table(‘payload’)->whereIn(‘id’, $headerpayload)->get(); This returns something like this #items: array:4 [▼ 0 => {#1726 ▼ +"id": 1 +"type": 1 +"name": "Xcalling ID" +"key_value": "X-CALLINGAPPID" +"status": ..
Laravel collections now have a great multi-level groupBy function built in, but I am struggling with a sensible way to insert new items. For example: $this->myCollection = EloquentModel::all()->groupBy([‘key1′,’key2′,’key3′,’key4’]); Very nice, easy to set up and access. For this example, I will assume each key is a number $this->myCollection[1][2][3][4] = new EloquentModel([insert => stuffHere]); If there ..
I have this simple array: [ ‘key1’ => ‘value’, ‘key2’ => ‘value’, ‘key3’ => [ ‘u-key1’ => ‘value’, ‘u-key2’ => ‘value’ ] ] I want to flatten it into this form: [ ‘key1’ => ‘value’, ‘key2’ => ‘value’, ‘u-key1’ => ‘value’, ‘u-key2’ => ‘value’ ] I’m sure there’s a simple way of doing it with ..
I want to insert or update rows into my models table. But can’t figure out the query. SmStudentAttendance This is my model. $students is my collection. I have put the collection fields in arrays. foreach ($students as $student) { array_push($temp_id, $student->id); array_push($temp_lastname, $student->last_name); array_push($temp_academic_id, $student->academic_id); array_push($temp_attendance, ‘P’); array_push($temp_attendancedate, $date); array_push($temp_schoolid, ‘1’); array_push($temp_updatedby, ‘1’); array_push($temp_createdby, ‘1’); ..
I have SQL query & sub query results and then convert these results to collection as bellow $co=collect($users); my collection array is as bellow IlluminateSupportCollection^ {#1984 #items: array:4 [ "id" => 92 "user" => "abc" "created_at" => "2020-04-16T12:13:11.000000Z" "updated_at" => "2020-12-05T20:32:56.000000Z" "groups" => array:6 [ 0 => array:6 [ "id" => 1 "user_id" => 1 ..
I have a problem with a query, I’m using Laravel 8 and my two tables are as follows: peoples: id | name | date_of_birth (DATE) | is_visible and other filds categories: id | name | min_age | max_age I would like to select all people with the category name if the difference between the date ..
I use the below code for pagination an array so for the first page everything is fine and the response is shown correctly but for the next pages, I have a problem, in the paginated array add a key per each object that I didn’t want that so how can I fix that? public function ..
My app uses Vue.js for the frontend and Laravel 8 for the backend. I am trying to build an array of objects from a Laravel collection selected from the database. I pull the collection from the model using: $explanations = Explanation::where(‘active’, true)->get(); which results in this collection (truncated): explanations: Array(28) 0: {id: 3, active: 1, ..
New to Eloquent and having a puzzling issue with regards to a return value from a method. The when() method works as expected for two of three almost identical filters, but the third one returns a results set that is formatted differently. I am able to filter a collection of results into two separate arrays ..
New to Laravel. Have what seems like should be a non-issue, but is causing a headache. I’m trying to insert a key-value pair (bookingRef) within the result object/collection returned, such that the result would be: [{"class_id":7,"class_name":"beginner","class_slots_avail":100,"class_slots_booked":53,"class_date":"2020-12-07 21:47:23","class_time":"09:25:00","class_reg_price":350, bookingRef: 127}] I’ve tried methods such as push, put and merge that will insert the key-value after the ..