laravel缓存使用

红太狼 2022-03-15 09:44 266阅读 0赞

模型中

  1. /***
  2. * 清除缓存
  3. */
  4. static function clear()
  5. {
  6. Cache::forget('shop_category');
  7. }
  8. /***
  9. * 查出所有数据并设置缓存
  10. * @return mixed
  11. */
  12. static function all_categories()
  13. {
  14. $categories = Cache::rememberForever('shop_category', function () {
  15. return self::with([
  16. 'children' => function ($query) {
  17. $query->orderBy('sort_order');
  18. }])->where('parent_id', 0)->orderBy('sort_order')->get();
  19. });
  20. return $categories;
  21. }

控制器中:

  1. function __construct()
  2. {
  3. view()->share([
  4. 'categories' => Category::all_categories()
  5. ]);
  6. }
  7. public function store(Request $request)
  8. {
  9. Category::create($request->all());
  10. Category::clear();
  11. return redirect(route('shop.categories.index'));
  12. }

发表评论

表情:
评论列表 (有 0 条评论,266人围观)

还没有评论,来说两句吧...

相关阅读

    相关 laravel篇之缓存

    简介 PHP缓存包括PHP编译缓存和PHP数据缓存两种。PHP是一种解释型语言,属于边编译边运行的那种。这种运行模式的优点是程序修改很方便,但是运行效率却很低下。PHP编译

    相关 laravel 使用jwt

    我们先写下我们的应用程序详细信息和功能。 我们将使用 JWT 身份验证在 laravel 中使用 restful API 构建基本用户产品列表。 A User 将会使用