Dingo Transformers 的使用(Fractal)
Post
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public function index($postId) { $post = $this->postRepository->find($postId);
if (! $post) { return $this->response->errorNotFound(); }
$comments = $this->postCommentRepository ->where(['post_id' => $postId]) ->paginate();
return $this->response->paginator($comments, new PostCommentTransformer()); }
|
这里主要需要说明的是, include的用法. include 的作用是,把附加信息带上.
比如你要查询post的评论.你只需要 http://jwt.cc/api/posts?include=user
带上个参数就可以了.
而不需要多加个路由. 这个参数不是随便起的,必须先定义.
这个方便是方便,不过还是有点疑问.就是缓存需要怎么做呢.所以这次追求速度就把这个方法舍弃了
还有就是带参数那个不知道怎么请求 public function includeComments(Post $post, ParamBag $params = null)
补充:
最近又看了一遍,又有了更全面的认识.他这里做到的表关联,其实还是用的laravel表关联. $post->user
其实就是表关联方法,就在model里.
经过测试 include 必须是关联表,因为我用 return 返回原表数据格式报错了.
看来一个表只能一种转换格式,不能有多个格式,这是问题啊.比如我一个页面有图片字段,另一个页面没有.
就不能通过多个格式返回来操作,只能是舍弃多的那个字段,不过要是区别很大的话就还是比较麻烦.
PostTransformers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| namespace ApiDemo\Transformers;
use ApiDemo\Models\Post; use League\Fractal\TransformerAbstract;
class PostTransformer extends TransformerAbstract {
protected $availableIncludes = ['user', 'comments'];
public function transform(Post $post) { return $post->attributesToArray(); }
public function includeUser(Post $post) { return $this->item($post->user, new UserTransformer()); }
public function includeComments(Post $post, ParamBag $params = null) { $limit = 10; if ($params) { $limit = (array) $params->get('limit'); $limit = (int) current($limit); }
$comments = $post->comments()->limit($limit)->get(); $total = $post->comments()->count();
return $this->collection($comments, new PostCommentTransformer())->setMeta(['total' => $total]); } }
|
Post
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| namespace ApiDemo\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends BaseModel { use SoftDeletes;
protected $casts = ['extra' => 'array'];
public function user() { return $this->belongsTo('ApiDemo\Models\User'); }
public function comments() { return $this->hasMany('ApiDemo\Models\PostComment'); } }
|
官方文档
Github说明