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

hjortur17's avatar

->through not working with Laravel and Inertia

Hi, I'm trying to add pagination to my site and I did follow both Jeffrey's tutorial and checked the demo app from inertia. They both used ->through for mapping there result but still getting the pagination along. I have in my component that posts should be an Object

Has anyone what is wrong with my code? (PHPStorm is complaining that fn only works in PHP 7.4)

Here is my code:

return Inertia::render('Posts/Index', [
            'filters' => Request::all('leita', 'flokkur', 'höfundur'),
            'tags' => Tag::all(),
            'posts' => Post::with(['tags', 'images'])
                ->orderBy('created_at')
                ->filter(Request::only('leita', 'flokkur', 'höfundur'))
                ->paginate(2)
                ->withQueryString()
                ->through(fn ($post) => [
                        'slug' => $post->slug,
                        'title' => $post->title,
                        'author' => $post->author,
                        'created_at' => $post->created_at,
                        'content' => $post->content,
                        'img_url' => $post->img_url,
                        'tags' => $post->tags,
                        'image' => $post->images[0]
                ])
        ]);
0 likes
20 replies
Sinnbeck's avatar

Oh first question. What php version are you running :)

Sinnbeck's avatar

@hjortur17 Good. Change php version down in the lower right corner, to have it stop yelling at you :)

Now what is the error you are getting in inertia?

hjortur17's avatar

@Sinnbeck - I do not see that option in PHPStorm. In my right corner I only see Event Log

hjortur17's avatar

@Sinnbeck - Changed this "php": "^7.3|^8.0", to this "php": "^7.4|^8.0", in my composer.json and ran the update. The error message went away but I'm still getting the same error on the web: post is null

Sinnbeck's avatar

@hjortur17 Good. Now onto the actual error. What is throwing the error? Vue? Laravel? Is there more text in the error message than those 3 words?

hjortur17's avatar

@Sinnbeck - Nothing more in the error message. Laravel is throwing this error, before I tried doing the pagination I was using ->transform but when I switched over to ->through I wasn't receiving any posts.

Here is how I was doing this with ->transform:

return Inertia::render('Posts/Index', [
            'filters' => Request::all('leita', 'flokkur', 'höfundur'),
            'tags' => Tag::all(),
            'posts' => Post::with(['tags', 'images'])
                ->filter(Request::only('leita', 'flokkur', 'höfundur'))
                ->get()
                ->transform(function ($post) {
                    return [
                        'slug' => $post->slug,
                        'title' => $post->title,
                        'author' => $post->author,
                        'created_at' => $post->created_at,
                        'content' => $post->content,
                        'img_url' => $post->img_url,
                        'tags' => $post->tags,
                        'image' => $post->images[0]
                    ];
                })
        ]);
Sinnbeck's avatar

@hjortur17 Hmm that should be fine.. Really strange that laravel/php is throwing an exception with only 3 words and nothing else. Can you post a screenshot?

Sinnbeck's avatar

@hjortur17 the error. You wrote

Nothing more in the error message. Laravel is throwing this error,

It really sounds like a Javascript error in the browser console, so I'm confused

hjortur17's avatar

@Sinnbeck - Sorry, but I'm not getting any error at the backend. VueJS is throwing the error because it's not receiving any posts. Sorry about that.

The error I'm getting is: post is null

Sinnbeck's avatar

Any chance you aren't doing post in posts.data?

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@hjortur17 you are missing .data

 
 ​            <​small-preview​ ​v-for​=​"​post ​in​ posts.data"​ :​key​=​"​post​.​title​"​ :​post​=​"​post​"​></​small-preview​> 
Sinnbeck's avatar

@hjortur17 no worries. Check the organizations/index file of the example and you can see how it's done in their looo

1 like

Please or to participate in this conversation.