Php Laravel函数未读取$id

Php Laravel函数未读取$id,php,laravel,laravel-5,Php,Laravel,Laravel 5,在我的web应用程序中,我添加了缓存方法。但问题是它没有读取传递到函数中的指定$id。所以它的抛出错误如下: ErrorException:未定义变量:id 如果有人提出任何答案,该怎么办将很有帮助 这是代码-> public function relatedstory($id) { $key = $id; $cacheKey = $this->getCacheKey($key); return cache()->remem

在我的web应用程序中,我添加了缓存方法。但问题是它没有读取传递到函数中的指定$id。所以它的抛出错误如下:

ErrorException:未定义变量:id

如果有人提出任何答案,该怎么办将很有帮助

这是代码->

public function relatedstory($id)
    {
        $key = $id;

        $cacheKey = $this->getCacheKey($key);

        return cache()->remember($cacheKey, Carbon::now()->addDay(1), function ()
        {
            $story = $this->_story->findOrFail($id);
            $tags = $story->tags->random(1)->pluck('tag_id');
            $storyIds = StoryTagItem::whereTagId($tags)->get()->random(4)->pluck('story_tag_id');
            $relatedStory = [];
            foreach($storyIds as $storyId)
            {
                array_push($relatedStory, $this->_story->findOrFail($storyId));
            }
            return StoryListResource::collection($relatedStory);
        });
    }
您需要
function()使用($id)
$id
注入到闭包中

return cache()->记住($cacheKey,Carbon::now()->addDay(1),函数()使用($id)
{
...
});
您需要
函数()使用($id)
$id
注入到闭包中

return cache()->记住($cacheKey,Carbon::now()->addDay(1),函数()使用($id)
{
...
});