Category : collections

Laravel I have a problem finding a data inside a collection which is filled with objects. My collection is look like this: return collect([ new Aligator(‘1’, ‘name1’), new Aligator(‘2’, ‘name2’), new Aligator(‘3’, ‘name3’)]; The data which I want to search is the name, given through url with contoller. Route::get(‘/aligator/{name}’, [AligatorController::class, ‘getAligator’]; After clicking the link ..

Read more

I have a method in my controller public function studentDirectory(Request $request, Student $student) { if($request->switch == ‘personal’) { return new StudentResource($student); } } Resource Controller: public function toArray($request) { return [ ‘id’ => $this->id, ‘name’ => $this->name, ‘address’ => $this->address, ‘phone’ => $this->phone, ‘tags’ => TagsResource::collection($this->tags)), ]; } Now this works fine and I get ..

Read more

I need to add pagination, but it seems complicated in my case, because I get data from database with paginate, but then I modify this data and when I call links() method on the blade, I get the following exception Method IlluminateSupportCollection::links does not exist. My code in the Controller method: $transactionsByLastMonth = Transaction::where(‘created_at’, ‘>=’, ..

Read more

I have a laravel collection below from: $unitinventory = DB::select(‘call wh_inventory_list(?)’, array(‘Units’)); [ { "unit_id":"UCL2100001", "rr_id":"RR2100001", "make":"FAW", "chassis_no":"LFWSRXRJ9M1E00004", "engine_no":"CA6DM2-42E5153558354", "body_type":"TRACTOR HEAD", "horse_power":"420HP", "cabin_type":"J6P E5", "numwheels":"6W", "unit_status":"Available", "unit_location":null }, { "unit_id":"UCL2100002", "rr_id":"RR2100002", "make":"FAW", "chassis_no":"LFWSRXRJ4M1E00007", "engine_no":"CA6DM2-42E5153563283", "body_type":"TRACTOR HEAD", "horse_power":"420HP", "cabin_type":"J6P E5", "numwheels":"6W", "unit_status":"Available", "unit_location":null } ] And another collection: $modifification = collect(DB::table(‘pd_jo_bodymodification’)->get()); [ { "row_id":2, "jo_id":"JO2100003", "jo_chassisno":"LFWSRXRJ4M1E00007", ..

Read more

I have two collections. First Collection is all descendants (not children) from root item, flat structure, in other words, ALL items in my DB in flat organization: $descendants = $root->getDescendants(); I go through this collection using foreach: foreach ($descendants as $key => &$item) {} For each item, I do some magic, but then, I need ..

Read more

I have an $attendace variable contains a collection from a Laravel query builder: $attendace = [ { "row_id":65, "emp_number":"IPPH0004", "time_stamp":"01:00:00", "attendance_status":"Punch In", "date_created":"2021-10-02" }, { "row_id":68, "emp_number":"IPPH0004", "time_stamp":"07:30:00", "attendance_status":"Start Break", "date_created":"2021-10-02" }, { "row_id":69, "emp_number":"IPPH0004", "time_stamp":"08:00:00", "attendance_status":"End Break", "date_created":"2021-10-02" }, { "row_id":70, "emp_number":"IPPH0004", "time_stamp":"08:30:00", "attendance_status":"Start Break", "date_created":"2021-10-02" }, { "row_id":71, "emp_number":"IPPH0004", "time_stamp":"09:00:00", "attendance_status":"End Break", "date_created":"2021-10-02" ..

Read more

I am trying to get the value from a collection in PHP. $todaylog variable contains a collection from a laravel query builder: $todaylog = [ { "row_id":55, "emp_number":"IPPH0004", "timestamp":"03:30:23", "attendance_status":"Punch In", "date_created":"2021-10-01" }, { "row_id":56, "emp_number":"IPPH0004", "timestamp":"11:32:50", "attendance_status":"Start Break", "date_created":"2021-10-01" }, { "row_id":57, "emp_number":"IPPH0004", "timestamp":"11:33:09", "attendance_status":"End Break", "date_created":"2021-10-01" } ] What I have done so ..

Read more

i have a ressource collection : class OverviewResource extends JsonResource { public function toArray($request): array { return [ ‘first_name’ => $this->first_name, ‘last_name’ => $this->last_name, ’email’ => $this->email, ‘phone’ => $this->phone, ‘friends’ => OverviewResource::collection(User::getFriends()), ]; } } i have an error in this line ‘friends’ => OverviewResource::collection(User::getFriends()), when calling this function from User model public static ..

Read more

In the following code, the "liked_by_me" column remains unchanged but the "newAttr" column is added. $commentList->transform(function($item, $key) { $item->liked_by_me == null ? $item->liked_by_me = false : $item->liked_by_me = true; $item->liked_by_me == null ? $item->newAttr = false : $item->newAttr = true; return $item; }); The $commentList collection is the result of a read from the DB ..

Read more

I have the following tables: users id name cattle_types id user_id name properties id user_id name properties_cattle_types id cattle_type_id property_id quantity I get all cattle types related to user through hasMany relation defined in user model: Auth::user()->cattleTypes But I need also take SUM of ‘quantity’ all notes that defined in pivot table properties_cattle_types. I don’t ..

Read more