dtxa49711 2018-10-03 09:12
浏览 17
已采纳

在今天的Laravel中,存储库是一个旧模式吗?

I'm trying to learn Laravel. I can see old docs and videos (from Laravel 5.4) speaking of Repositories and repository pattern, meaning classes to work with collections of data. But on the current (5.7) Laravel docs, I cannot find any reference to Repositories or the pattern itself. So I'm wandering: are still used (and thus I should study them) or maybe the 'new' ones are what it's called 'Collections'? Or maybe they are not at all related?

  • 写回答

1条回答 默认 最新

  • dongxian3852 2018-10-03 09:25
    关注

    Repositories are not implemented by default on Laravel. If you want to use them you need to implement yourself, or use a package. The repository pattern is a design pattern mainly use to abstract data access by returning standard classes for exemple, so this way if you decide to change your ORM from Eloquent to Doctrine, you will have to change only the repository, and the rest of your application will be fine. That help to build scalable and maintenable applications.

    Now this is not mandatory at all, it's just good to know what it is and how to use it. But if your application is small, my advice would be : Don't bother with Repository. That's just gonna add more complexity to your code. But this is a really famous design pattern, so that's probably why it is in a lots of video and tutorials.

    Collection : In Laravel, Collections are an object returned by the query builder when a query return more than one result. Collections have a lots of really handy methods to help you manipulate these data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置