dtxa49711 2018-10-03 01: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 01: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部