Laravel 变压器与资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52378729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:03:46  来源:igfitidea点击:

Laravel transformer vs resource

phplaravellaravel-resource

提问by Emtiaz Zahid

I am little bit confused about the difference between Laravel eloquent resource and transformers. When i should use resource and when i should use transformers. Did some research on it from laravel documentationand medium articlebut its look like same purpose.

我对 Laravel eloquent resource 和 Transformers 之间的区别有点困惑。什么时候应该使用资源,什么时候应该使用变压器。从laravel 文档中等文章中对它进行了一些研究,但它看起来目的相同。

May be i have wrong concept about that. Any knowledge share from anyone will be appreciate.

可能是我对此有错误的概念。任何人分享的任何知识都将不胜感激。

回答by Elias Soares

Laravel Eloquent API Resourcewas released on Laravel 5.5.

Laravel Eloquent API Resource在 Laravel 5.5 上发布。

Before this feature exists officially, there was a package from spatie called spatie/laravel-fractalthat's a wrap for league/fractal.

在此之前,功能正式存在,有一个从spatie了一个叫做spatie/laravel-fractal这对一个包裹league/fractal

Both packages archives the same objective with different approach.

两个包都使用不同的方法归档相同的目标。

I prefer using the Eloquent API Resource since it's embedded on Laravel, so I reduce one (direct) dependency.

我更喜欢使用 Eloquent API Resource,因为它嵌入在 Laravel 中,因此我减少了一个(直接)依赖项。

Note:The medium article uses the Fractalpackage, but this article is very superficial, you can find more detailed instructions on spatie/laravel-fractal, it you decide to use it.

注:中篇文章使用了Fractal包,但是这篇文章很肤浅,你可以在上面找到更详细的说明spatie/laravel-fractal,你决定使用它。

回答by David Valenza

Found this on a google search below your post:

在您帖子下方的谷歌搜索中找到了这个:

You build out each "transformer" (which would be the equivalent of a "resource"), with the ability to include or not include whichever relationships you want solely based off the of request URL.

您构建了每个“转换器”(相当于“资源”),能够包含或不包含您想要的任何关系,仅基于请求 URL。