Category : collections

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; ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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(); } } ..

Read more

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 ..

Read more

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": ..

Read more

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’ => ..

Read more

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 ..

Read more

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: ..

Read more

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" } ..

Read more

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 ..

Read more

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 ..

Read more

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’); ..

Read more

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 ..

Read more

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, ..

Read more

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 ..

Read more