0

I have just started my journey with Laravel and PHP.

I have this route:

Route::get('/admin/applications/staff-role', function () {
    $staff = StaffRole::with('applications')->where('id', '=', '1')->get();
    return $staff;
});

which is the result of hasManyThrough query and returns this result:

[
    {
        "id": 1,
        "staff_role_label": "Staff 1",
        "staff_role_description": "Staff Role 1",
        "staff_role_abbreviation": "SR1",
        "deleted_at": null,
        "applications": [
            {
                "id": 1,
                "vacancy_id": 1,
                "hours_per_week": "9",
                "contract_type": "Full-Time",
                "desired_shift_option": "Weekends",
                "name": "John",
                "surname": "Doe",
                "mobile_phone_number": "07911 123456",
                "email": "test1@test.com",
                "application_status": 1,
                "is_staff": 1,
                "staff_status": 0,
                "deleted_at": null,
                "laravel_through_key": 1
                },
                {
                "id": 2,
                "vacancy_id": 1,
                "hours_per_week": "9",
                "contract_type": "Full-Time",
                "desired_shift_option": "Weekends",
                "name": "Jane",
                "surname": "Doe",
                "mobile_phone_number": "07911 123456",
                "email": "test2@test.com",
                "application_status": 1,
                "is_staff": 1,
                "staff_status": 0,
                "deleted_at": null,
                "laravel_through_key": 1
            }
        ]
    }
]

So far, so good, but how can I count how many applications I have in the array?

Thanks.

UB101
  • 21
  • 3
  • What have you tried? Laravel and PHP both have methods for counting items in a Collection and Array respectively. – Tim Lewis Sep 10 '21 at 19:41
  • If I try Route::get('/admin/applications/staff-role', function () { $staff = StaffRole::with('applications')->where('id', '=', '1')->get(); return $staff->count(); }); It will return only 1 because is related to the 'staff role' and not the 'applications' – UB101 Sep 10 '21 at 19:45
  • Well yeah, `$staff` is the Collection of `StaffRole` instances. I'll give you a hint; `->get()` returns a Collection of multiple Rows, but since you're using `where('id', '=', 1)`, maybe you want `first()` instead? Then `$staff->applications->count()`, perhaps? – Tim Lewis Sep 10 '21 at 19:46
  • @TimLewis $staff->applications->count() returns an error 'Property [applications] does not exist on this collection instance.' – UB101 Sep 10 '21 at 19:53
  • Read my whole comment please; use `->first()` instead of `->get()`. You need to learn what the difference is between a single instance of `StaffRole` and a `Collection` of `StaffRole` instances (i.e. what is returned from `->get()` vs `->first()`) – Tim Lewis Sep 10 '21 at 19:53
  • No problem. https://laravel.com/docs/8.x/collections#method-count, https://www.php.net/manual/en/function.count.php and https://stackoverflow.com/questions/41366092/property-title-does-not-exist-on-this-collection-instance, 3 thinks you could have searched to solve your issue. Please try some research and self-debugging in the future; help you help yourself – Tim Lewis Sep 10 '21 at 20:00

0 Answers0