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

nhayder's avatar
Level 13

how to chain relationships with query builder

i'm trying to get editor and poster data for every article in single query, unfortunately i'm not getting these data on the final query.

this is my code

    public function widgets($id = null)
    {

        if($id){

            $pageID = $id;

        }else{

            $id = 1;

        }

        $widgets = Widget::with(
                
                    ['category.articles' => function ($query) {

                        $query->where('language', 'en');

                    }],

                    'category.articles.editor',

                    'category.articles.poster',

                    'buttons', 'content', 'content.linkedPage', 'content.linkedArticle'

                )

            ->where('page_id', '=', $id)

            ->orderBy('sorting', 'asc')
            
            ->get();

        return $widgets;

    }

After running the code i'm not returning the editors neither the poster data.

is there is a better way on how to this type of query.

any ideas ???

0 likes
4 replies
ftiersch's avatar
ftiersch
Best Answer
Level 28

You should be able to put it in your closure:

 ['category.articles' => function ($query) {

                        $query->where('language', 'en')
                ->with('editor', 'poster');

                    }],
1 like
nhayder's avatar
Level 13

hey @ftiersch how is your day,

yes it worked and i can see the editor and poster data, which is a great improvement.

but still i lost the rest of the relationships (buttons, content, ... etc)

        $widgets = Widget::with(
                
                    ['category.articles' => function ($query) {

                        $query->where('language', 'en')
                        ->with('editor', 'poster');

                    }],

                    'buttons', 'content', 'content.linkedPage', 'content.linkedArticle'

                )

            ->where('page_id', '=', $id)

            ->orderBy('sorting', 'asc')
            
            ->get();

        return $widgets;

any ideas

1 like
nhayder's avatar
Level 13

@ftiersch i was able to get all the data like this

        $widgets = Widget::with(['buttons', 'content', 'content.linkedPage', 'content.linkedArticle','category.articles' => function ($query) {

                        $query->where('language', 'ar')
                        ->with('editor', 'poster');

                    }]


                )

            ->where('page_id', '=', $id)

            ->orderBy('sorting', 'asc')
            
            ->get();

        return $widgets;

Thanx man

1 like
Sti3bas's avatar

@nhayder other relationships should also be in array:

with([
    'category.articles' => function ($query) {
        $query->where('language', 'en')->with('editor', 'poster');
    }, 'buttons', 'content', 'content.linkedPage', 'content.linkedArticle'
])
1 like

Please or to participate in this conversation.