如何从Laravel缓存中获取Laravel媒体库包的数据

时间:2019-07-19 18:21:59

标签: php laravel caching media-library

创建帖子时,我使用Laravel媒体库上传照片,我可以使用以下方法从媒体库中检索数据:

$blog->getFirstMediaUrl('blog');

我使用Laravel缓存功能来获得最佳性能,并通过redis进行最少查询。我将博客数据存储在缓存中,如下所示:

$blogs=Blog::where('publication_status', 1)
->select('id', 'admin_id', 'category_id', 'sub_category_id', 
'blog_section_id', 'blog_title', 'slug',
'blog_short_description', 'blog_long_description', 'author_name',
'thumbnail', 'publication_status', 'created_at')
->orderBy('id', 'DESC')
->get();

Cache::forever('blogs', $blogs);

那时我缓存了媒体库照片,如下所示:

$media=Media::all(); Cache::forever('media', $media);

之后,我在前端检索了我的博客数据,如下所示:

$data['blogs']=cache()-get('blogs', function (){
    Blog::where('publication_status', 1)
    ->select('id', 'admin_id', 'category_id', 'sub_category_id', 'blog_section_id', 'blog_title', 'slug',
    'blog_short_description', 'blog_long_description', 'author_name',
    'thumbnail', 'publication_status', 'created_at')
    ->orderBy('id', 'DESC')
    ->get();
});

return view('Frontend.includes.homepage.homepage', $data);

并在视图文件中传递$ data,如下所示:

<div class="single_post_content_left">
    <ul class="business_catgnav  wow fadeInDown">
        @foreach($blogs
        ->where('thumbnail', 1)
        ->where('blog_section_id', 1)
        ->take(1)
        as $main_section)

        <li>
            <figure class="bsbig_fig"> <a href="{{ route('article', $main_section->slug) }}" class="featured_img"> <img alt="" src="{{
                $main_section->getFirstMediaUrl('blog') }}"> <span
                class="overlay"></span> </a>

                <figcaption> <a href="{{ route('article', $main_section->slug) }}">{{ $main_section->blog_title }}</a>
                </figcaption>
                <p>{{ $main_section->blog_short_description }}</p>
            </figure>
        </li>

        <small class="pull-right"><i class="fa fa-clock-o"></i> {{ $main_section->created_at->diffForHumans() }}</small>

        @endforeach
    </ul>
</div>

此处成功从缓存中检索博客,而没有媒体库数据:

$main_section->getFirstMediaUrl('blog');

但是我想从缓存(如博客)中检索媒体库数据,以使查询运行最少,该怎么办?

0 个答案:

没有答案