Z
Z
zytia2022-04-02 15:10:44
PHP
zytia, 2022-04-02 15:10:44

How to remove an object using a condition from the data received from a request with pagination?

Task: you need to get a students object, with the conditions group id and specialty id, where all these 3 tables are connected. It is also necessary to paginate only those objects of students for whom a group and specialty was found.

The code:

$students = Student::with([
            "group" =>
                function($query) {
                    $query->where("id", \request("group_id"));
                },
            "group.speciality" =>
                function($query) {
                    $query->where("id", \request("speciality_id"));
                },
            ])->paginate(\request("page_size") ? : 10)->toArray();

return response()->json($students);


Problem: if the group or specialty data is not found, then the object is still added with the group: null attribute to pagination, thereby failing to display the requested number of elements on the page. How can this problem be solved?

Upon request
group_id = 2
speciality_id = 2
page_size = 2


The following object is returned:
{
    "current_page": 1,
    "data": [
        {
            "id": 1,
            "receipt_date": "2010-11-02",
            "user": {
                "id": 1,
                "login": "[email protected]",
                "phone": "+7 (922) 472-9240",
                "role": "user",
                "passport": {
                    "series": 1762,
                    "number": 384282,
                    "date_of_issue": "1991-11-27",
                    "issued": "magni",
                    "division_code": 3,
                    "scan": "*photo link*",
                    "secondname": "Куликова",
                    "firstname": "Ольга",
                    "thirdname": "Анисимова",
                    "birthday": "1973-05-13",
                    "sex": "W"
                }
            },
            "group": {
                "id": 2,
                "group_code": "4433",
                "speciality": {
                    "id": 2,
                    "specialty_title": "Программирование в компьютерных системах",
                    "faculty": "СПО ИКТЗИ"
                }
            }
        },
        {
            "id": 2,
            "receipt_date": "1973-11-07",
            "user": {
                "id": 2,
                "login": "[email protected]",
                "phone": "+7 (922) 903-0339",
                "role": "user",
                "passport": {
                    "series": 8241,
                    "number": 419233,
                    "date_of_issue": "1980-06-05",
                    "issued": "quos",
                    "division_code": 33,
                    "scan": "*photo link*",
                    "secondname": "Ефремов",
                    "firstname": "Болеслав",
                    "thirdname": "Костин",
                    "birthday": "2009-04-03",
                    "sex": "W"
                }
            },
            "group": null
        }
    ],
    "per_page": "2",
    "total": 75
}


Whereas with group == null, only 2 objects with groups should be returned

{
    "current_page": 1,
    "data": [
        {
            "id": 1,
            "receipt_date": "2010-11-02",
            "user": {
                "id": 1,
                "login": "[email protected]",
                "phone": "+7 (922) 472-9240",
                "role": "user",
                "passport": {
                    "series": 1762,
                    "number": 384282,
                    "date_of_issue": "1991-11-27",
                    "issued": "magni",
                    "division_code": 3,
                    "scan": "*photo link*",
                    "secondname": "Куликова",
                    "firstname": "Ольга",
                    "thirdname": "Анисимова",
                    "birthday": "1973-05-13",
                    "sex": "W"
                }
            },
            "group": {
                "id": 2,
                "group_code": "4433",
                "speciality": {
                    "id": 2,
                    "specialty_title": "Программирование в компьютерных системах",
                    "faculty": "СПО ИКТЗИ"
                }
            }
        },
        {
            "id": 5,
            "receipt_date": "2002-07-05",
            "user": {
                "id": 5,
                "login": "[email protected]",
                "phone": "+7 (800) 555-3535",
                "role": "user",
                "passport": {
                    "series": 5521,
                    "number": 866521,
                    "date_of_issue": "1980-06-05",
                    "issued": "quos",
                    "division_code": 33,
                    "scan": "*photo link*",
                    "secondname": "Павлов",
                    "firstname": "Денис",
                    "thirdname": "Артемьев",
                    "birthday": "2009-04-03",
                    "sex": "W"
                }
            },
            "group": {
                "id": 2,
                "group_code": "4433",
                "speciality": {
                    "id": 2,
                    "specialty_title": "Программирование в компьютерных системах",
                    "faculty": "СПО ИКТЗИ"
                }
            }
        }
    ],
    "per_page": "2",
    "total": 75
}


The data presented is irrelevant as it is randomly generated.

Removing data elements in a loop with group equal to null does not solve the problem, since pagination gets lost.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pLavrenov, 2022-04-03
@zytia

You need to use whereHas which will add the condition group.id == 2, then only students with group relationships with the desired conditions will be obtained.

V
vitaly_74, 2022-04-02
@vitaly_74

I think that with is the same left join that returns null if there is no connection, try instead of left Join, insert a right join with the same conditions
or add a where
WHERE condition
group.id IS NOT NULL and speciality.id IS NOT NULL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question