Category : collections

I want to pass an additional parameter from controller to collection resource, Please have a look at the following code, I don’t know why It’s not working. I might be doing something wron here, please help Controller $batchStudents = $batch->students->except($batch->host->id); $batchStudents->test = ‘This is testing’; $students = UserCollection::collection($batchStudents); Collection: class UserCollection extends JsonResource { return ..

Read more

I get the error in my collect and I dont know how to fix it. require "vendor/autoload.php"; use IlluminateSupportCollections; class CityAnalyzer { private $cityData; public function __construct($cityData) { $this->$cityData = $cityData; } public function printHighestPopulatedCities() { $sortedDescendingCityData = collect($this->$cityData)->map(function($city) { return new City($city["place_name"], $city["total_population_2010"]); })->sortByDesc(‘population’)->values()->all(); for ($i = 0; $i < 10; $i ++) { ..

Read more

I have StudentEducation model and in this model i got public function student_education_countries() { return $this->hasMany(StudentEducationCountry::class, ‘education_id’); } and public function student_education_education_types() { return $this->hasMany(StudentEducationEducationType::class, ‘education_id’); } in StudentEducationCountry model i got public function student_education() { return $this->belongsTo(StudentEducation::class, ‘education_id’); } public function student_country() { return $this->belongsTo(StudentCountry::class, ‘country_id’); } and in StudentEducationEducationType model i got public ..

Read more

I have a problem with usage laravel pagination with collection method. My Code: $items = UserItems::whereHas(‘item.adviser’ , function($query) use($request){ return $query->where(‘id’, $request->adviser_id); }) ->with([‘item.adviser’, ‘user:id,firstname,lastname’]) ->orderByRaw(‘ISNULL(answer) DESC, answer DESC’) ->paginate(10); And Collection method: $items = $items->each(function ($item, $key){ if($item->answer !== null){ $item->answer = true; } else { $item->answer = false; } }); But when i ..

Read more

Here is my code: return ApiResponse::Json(200, ”, [‘categories’ => $categories], 200); And here is a screenshot of the result: Now, I need to unset items inside categories collection based on a specific logic. So I wrote this loop: foreach ($categories as $key => $category) { if ($category->BusinessSubCategory->isEmpty()) unset($categories[0]); } return ApiResponse::Json(200, ”, [‘categories’ => $categories], ..

Read more

I’m pulling data from Airtable, which returns like so: IlluminateSupportCollection {#496 ▼ #items: array:11 [▼ 0 => array:3 [▼ "id" => "rec4CaAx5EwGGi0I3" "fields" => array:8 [▼ "description" => "Soft covers for switches" "price" => 7 "tags" => array:2 [▼ 0 => "recvPyxXM6GdIjgEr" 1 => "recvskfZPFMRPXzEb" ] I can find how to convert to: Collection of ..

Read more

I have these arrays of arrays $firstCollection = [ [‘id’ => 11, ‘data’ => ‘a’], [‘id’ => 22, ‘data’ => ‘b’], [‘id’ => 33, ‘data’ => ‘c’] ]; $secondCollection = [ [‘num’ => 11, ‘txt’ => ‘is first’], [‘num’ => 22, ‘txt’ => ‘is second’], [‘num’ => 33, ‘txt’ => ‘is third’] ]; my expected ..

Read more

I’m trying to create Collection to return spefific fields on API request on Laravel 8. My Resource file: public function toArray($request) { return [ ‘name’ => $this->name ]; } When I’m trying to get all Users I’m getting this error : Property [name] does not exist on this collection instance. My api.php Route::get(‘/users’, function () ..

Read more

I’m trying to paginate my collection that I’ve generated with below codes $vehicles = Vehicle::whereHas(‘destinations’, function($q) use($id){ $q->where(‘destination_id’,$id) ->where(‘active’,1); })->get(); The collection i’ve recieved from above is looped again. foreach ($vehicles as $key => $vehicle) { # code… $perday_rate = $vehicle->destinations->where(‘id’,$destination_id)->first()->pivot->day_rate; $pernight_rate = $vehicle->destinations->where(‘id’,$destination_id)->first()->pivot->night_rate; $day_rate = $perday_rate*$days; $night_rate = $pernight_rate*$nights; $total_amount = $day_rate+$night_rate; $vehicle[‘total_amount’] = ..

Read more

I’m trying to make several collections from one collection. What I do is filtering and mapping a clone of the collection: $nutrients = $this->get_records_daily_average($request); // Make & filter first copy $Vitamins = clone $nutrients; $Vitamins = $Vitamins->filter(function($nutrient) { return in_array($nutrient->id,config(("hooman.nutrient_ids.score_related.Vitamins"))); })->map(function($nutrient){ $nutrient->score_category= "Vitamins"; return $nutrient; })->values(); //Make & Filter Another copy $immune_support = clone $nutrients; ..

Read more

Assuming the following code to represent an order and the related transactions: order public function transactions(){ return $this->hasMany(‘AppTransaction’); } already loaded collection ($orders) order1 id amount transactions (relation) txid method amount order2 id amount transactions (relation) txid method amount The following filtering on already loaded collection does not work as expected: if($orders->where(‘transactions.txid’, ‘=’, $receivingPayment->txid)->isNotEmpty()) dd("hit"); ..

Read more

I want to make a Collection macro function in the service provider. The problem is after importing the Collection class i can not access the collection funtion $this->values() But when I use the same code in the controller is working fine. <?php namespace AppProviders; use IlluminateSupportServiceProvider; use IlluminateSupportCollection; class CollectionExtensions extends ServiceProvider { /** * ..

Read more

I have a nested collection that I want to transform, pulling some keys "up a level" and discarding some other keys. Every item in the collection has an allergens property. "allergens": [ { "id": 2001, "info": "Allergy advice", "status": "does_contain", "product_id": 71576, "allergen_id": 1, "allergen": { "id": 1, "name": "Celery" } }, { "id": 2002, ..

Read more

When I try to update the data it gives an error that; Method IlluminateDatabaseEloquentCollection::update does not exist. How can I solve this? Here is my SubjectController.php public function update(Request $request, Subject $id) { $subjects = Subject::find($id); if(!$subjects){ return back()->with(‘Error’, ‘Subject not Found’); } $subjects->update($request->all()); return back()->with(‘Success’, ‘Subject Updated Successfully’); } Here my web.php Route::resource(‘subjects’, SubjectController::class); ..

Read more

I have a collection grouped (multiple times) like this: IlluminateSupportCollection {#1883 ▼ #items: array:1 [▼ 57082 => IlluminateSupportCollection {#1885 ▼ #items: array:1 [▼ "07-2021" => IlluminateDatabaseEloquentCollection {#1863 ▼ #items: array:1 [▼ 343 => IlluminateDatabaseEloquentCollection {#1864 ▼ #items: array:1 [▶] } ] } ] } ] } I have another collection that could have same values ..

Read more