laravel eloquent 模型缓存

2021-12-26 00:00:00 php laravel laravel-5 eloquent

我正在Laravel 5.6中学习Laravel eloquent.

I am learning laravel eloquent in Laravel 5.6.

我已经使用过数据库查询构建器 &缓存.

I have already used database query builder & cache.

检索 &用于数据库查询生成器的存储

$value = Cache::remember('users', $minutes, function () {
    return DB::table('users')->get();
});

但我不知道如何缓存 eloquent.

But i don't know how to cache for eloquent.

检索 &为 Eloquent 存储

$value = Cache::remember('users', $minutes, function () {
        return AppUser::all();
    });

eloquent 或任何其他现有方法是否正确?

Is it correct for eloquent or any other method existing?

推荐答案

有一个内置的方法:

AppUser::where(['id' => 1])->cache()

我希望这会有所帮助,尽管一年后.

I hope this helps, although a year later.

相关文章