@tykus
with resource
{
"status": true,
"code": 200,
"message": "Trainers fetched successfully!",
"data": [
{
"id": 6,
"name": "jatt",
"email": "[email protected]",
"email_verified_at": null,
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z",
"details": {
"id": 5,
"user_id": 6,
"height": 132,
"weight": 43,
"country_code": null,
"phone_number": "979887989",
"gender": "male",
"status": "inactive",
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z"
},
"documents": [
{
"id": 1,
"media_id": 11,
"user_id": 6,
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z"
}
],
"pictures": [
{
"id": 1,
"media_id": 9,
"user_id": 6,
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z"
},
{
"id": 2,
"media_id": 10,
"user_id": 6,
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z"
}
]
}
]
}
without resource
{
"status": true,
"code": 200,
"message": "Trainers fetched successfully!",
"data": {
"current_page": 1,
"data": [
{
"id": 6,
"name": "jatt",
"email": "[email protected]",
"email_verified_at": null,
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z",
"details": {
"id": 5,
"user_id": 6,
"height": 132,
"weight": 43,
"country_code": null,
"phone_number": "979887989",
"gender": "male",
"status": "inactive",
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z"
},
"documents": [
{
"id": 1,
"media_id": 11,
"user_id": 6,
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z"
}
],
"pictures": [
{
"id": 1,
"media_id": 9,
"user_id": 6,
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z"
},
{
"id": 2,
"media_id": 10,
"user_id": 6,
"created_at": "2025-02-17T08:32:46.000000Z",
"updated_at": "2025-02-17T08:32:46.000000Z"
}
]
}
],
"first_page_url": "http://127.0.0.1:8000/api/v1/admin/trainer?page=1",
"from": 1,
"last_page": 2,
"last_page_url": "http://127.0.0.1:8000/api/v1/admin/trainer?page=2",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://127.0.0.1:8000/api/v1/admin/trainer?page=1",
"label": "1",
"active": true
},
{
"url": "http://127.0.0.1:8000/api/v1/admin/trainer?page=2",
"label": "2",
"active": false
},
{
"url": "http://127.0.0.1:8000/api/v1/admin/trainer?page=2",
"label": "Next »",
"active": false
}
],
"next_page_url": "http://127.0.0.1:8000/api/v1/admin/trainer?page=2",
"path": "http://127.0.0.1:8000/api/v1/admin/trainer",
"per_page": 5,
"prev_page_url": null,
"to": 5,
"total": 10
}
}
My code
function get($id = null,$search = null)
{
try {
$query = User::with('details', 'documents', 'pictures')
->where('id', '!=', 1);
if ($id) {
return $query->where('id', $id)->firstOrFail();
}
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('name', 'like', '%' . $search . '%')
->orWhere('email', 'like', '%' . $search . '%')
->orWhereHas('details', function ($detailsQuery) use ($search) {
$detailsQuery->where('phone_number', 'like', '%' . $search . '%');
});
});
}
//return TrainerResource::collection( $query->paginate(5));
return $query->paginate(5);
} catch (\Throwable $th) {
throw new ApiException('An error occurred while getting trainer details.', 400, $th, "TrainerService : get");
}
}