Answer the question
In order to leave comments, you need to log in
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);
group_id = 2
speciality_id = 2
page_size = 2
{
"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
}
{
"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
}
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question