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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大