Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AsnCode's avatar

Retrieve only specific column in inertia with relationships

Hello i,ve build an app in Laravel 10 and Inertia ( vue.js ), but i need to hide certain information in my front end :

I've this on relationship [User hasMany Capsules] - [Capsule belongsTo User] - [user hasOne Avatar ] and more nest ressource but in my front end i can see user created_at, updaed_at, is_admin and more ... ^^

How can i select just some specific colum to be showed in vue console debugger ?? Im stuck with how can i put relationship in ->select() query ??!!

My controller look like :

$capsules   = Capsule::query()
            ->where('published', true)
            ->select(['title', 'slug', 'category_id', 'user_id', 'city_id', 'catch_phrase', 'practice', 'gender', 'price', 'capacity', 'postal_code'])
            ->filterByPriceFrom($filters)
            ->filterByPriceTo($filters)
            ->filterByCategory($filters)
            ->filterByCity($filters)
            ->filterByPractice($filters)
            ->filterByGender($filters)
            ->filterByCreatedAt($filters)
            ->filterByPriceSort($filters)
            ->filterBySearch($filters)
            ->with(
                [
                    'user' => function($query) {
                        $query->withCount([
                            'followers as following' => function($query){
                                $query->where('follower_id', Auth::id());
                            },
                            'followings as follower' => function($query){
                                $query->where('followed_id', Auth::id());
                            },
                            'capsules', 'followers'
                        ]);
                    },
                ]
            )
            ->withCount([
                'likes as liked' => function($query){
                    $query->where('user_id', Auth::id());
                }, 'likes'
            ])
            ->withCasts(['liked' => 'boolean', 'follower' => 'boolean', 'following' => 'boolean'])
            ->mostRecents()
            ->paginate(50)
            ->onEachSide(5)
            ->withQueryString();

I see all the series of Inertia here but nothing to hide column inside relation model. It is possible to hide certain column like for exemple directly in Model definition ? Thanks you !

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You are seeing the user-specific fields on the user property I suppose? So, select within the eager-loaded relationship:

// ...
->with(
                [
                    'user' => function($query) {
                        $query->select(/* user fields you want/need */)
                            ->withCount([
                                'followers as following' => function($query){
                                    $query->where('follower_id', Auth::id());
                                },
                                'followings as follower' => function($query){
                                    $query->where('followed_id', Auth::id());
                                },
                                'capsules', 'followers'
                            ]);
                    },
                ]
            )
// ...

I would always recommend to build Eloquent API Resources for these scenarios; you then have complete control over the data being sent to the client-side.

1 like
AsnCode's avatar

@tykus work like a charm :) for the advice, I think i will put inside a ressource in the future

thanks you ;)

Please or to participate in this conversation.