Is there any way that I can use to mapping all this data? from the scope query of model. So I just want to hide the data of email & phone number in the comment user.
{
"id": 1,
"name": "Hello Tayos",
"address": "Hello World",
"description": "Hello World",
"status": 1,
"created_at": "2024-08-29T09:55:47.000000Z",
"updated_at": "2024-08-29T10:05:04.000000Z",
"user": {
"id": 1,
"role_id": 1,
"desa_id": null,
"email": "",
"status": 1,
"email_verified_at": "2024-08-29T08:50:22.000000Z",
"created_at": "2024-08-29T08:50:23.000000Z",
"updated_at": "2024-08-29T08:50:23.000000Z",
"profile": {
"id": 1,
"user_id": 1,
"name": "",
"birth_date": null,
"place_of_birth": null,
"address": null,
"phone": "",
"gender": "",
"photo": null,
"created_at": "2024-08-29T08:50:23.000000Z",
"updated_at": "2024-08-29T08:50:23.000000Z"
}
},
"comments": [
{
"id": 2,
"kejadian_id": 1,
"user_id": 1,
"comment": "Bagaimana",
"created_at": "2024-08-29T10:01:12.000000Z",
"updated_at": "2024-08-29T10:01:12.000000Z",
"user": {
"id": 1,
"role_id": 1,
"desa_id": null,
"email": "",
"status": 1,
"email_verified_at": "2024-08-29T08:50:22.000000Z",
"created_at": "2024-08-29T08:50:23.000000Z",
"updated_at": "2024-08-29T08:50:23.000000Z",
"profile": {
"id": 1,
"user_id": 1,
"name": "",
"birth_date": null,
"place_of_birth": null,
"address": null,
"phone": "",
"gender": "",
"photo": null,
"created_at": "2024-08-29T08:50:23.000000Z",
"updated_at": "2024-08-29T08:50:23.000000Z"
}
}
}
]
}
#How to mapping data object in laravel?
7 messages · Page 1 of 1 (latest)
https://laravel.com/docs/11.x/eloquent-resources#main-content
https://laravel.com/docs/11.x/collections#method-map
what have you tried? share some codes? or you could just use the methods mentioned in the shared links
I tried something like this, so I want to hide several columns when attached the relationships.
class Kejadian extends Model {
// Scope untuk mengambil data dengan relasi terkait
public function scopeWithRelations(Builder $query)
{
return $query->with([
'user.profile',
'comments' => function ($query) {
$query->with('user.profile')->orderBy('id', 'desc')->limit(1);
}
]);
}
}
Is it possible to make it in scope?
https://laravel.com/docs/11.x/eloquent-relationships#eager-loading-specific-columns
you could load the neccesarry columns only, then use the API resources to filter out the foreign keys
If phone and email should always be hidden when serialized to array or json, just add this to your profile model
protected $hidden = ['email', 'phone'];
No sir, it won't be always hidden. Only on a few.