【问题标题】:Retrieving eloquent api resource using keyby collection method使用 keyby 收集方法检索 eloquent api 资源
【发布时间】:2020-01-29 18:27:27
【问题描述】:

我有一个结束 API 点

users/{user}

现在在用户资源中,我想返回

     public function toArray($request)
        {
            // return parent::toArray($request);

            return [
                'id' => $this->id,
                'name' => $this->name,
//                'comments' => $this->post->comments->keyBy('post_id')
                'comments' => new CommentCollection($this->post->comments->keyBy->post_id)

            ];
        }

CommentCollection 类

public function toArray($request)
    {
        // return parent::toArray($request);

        return [
            'data' => $this->collection->transform(function($comment){
                return [
                    'id' => $comment->id,
                    'comment' => $comment->comment,
                ];
            }),
        ];
    }

但结果不会包含 post_id 作为键,我怎样才能让它返回具有键 post_id 的 cmets 集合?

更新

use App\models\Post;
use App\Http\Resources\Postas PostResource;

Route::get('/posts', function () {
    return PostResource::collection(Post::all()->keyBy->slug);
});

这工作正常,但如果我将用户资源中的帖子集合用作关系,它就不起作用!这就是我对 cme​​ts 收藏的要求。

【问题讨论】:

    标签: php laravel laravel-5 eloquent laravel-resource


    【解决方案1】:

    我做了什么,我创建了另一个 ResourceGroupCollection 类

    <?php
    namespace App\Http\Resources\Collection;
    
    use Illuminate\Http\Resources\Json\ResourceCollection;
    
    class CommentGroupCollection extends ResourceCollection
    {
        public $collects = 'App\Http\Resources\Collection\CommentCollection';
    
        public $preserveKeys = true;
    
        public function toArray($request)
        {
            return $this->collection;
        }
    
    }
    
    <?php
    namespace App\Http\Resources\Collection;
    
    use Illuminate\Http\Resources\Json\ResourceCollection;
    
    class CommentCollection extends ResourceCollection
    {
        public $collects = 'App\Http\Resources\Comment';
    
        public $preserveKeys = true;
    
        public function toArray($request)
        {
            return $this->collection;
        }
    
    }
    
    and then 
    
    new CommentGroupCollection($comments->groupBy('post_id')),
    

    【讨论】:

      【解决方案2】:

      就像这样:

           public function toArray($request)
              {
                  // return parent::toArray($request);
      
                  return [
                      'id' => $this->id,
                      'name' => $this->name,
      //                'comments' => $this->post->comments->keyBy('post_id')
                      'comments' => new CommentCollection($this->post->comments)->keyBy('post_id')
      
                  ];
              }
      
      

      【讨论】:

      • 你可以看到,我有确切的代码但它不起作用,这就是我发布问题的原因。
      • 你能发布你的CommentCollection类吗?
      • 不是toArray函数(),在你的情况下不会用到,它是如何构造的?你有一个 post_id 列吗?你能 dd(new CommentCollection($this->post->cmets)); ?
      • 我有一些关于我的问题的更新,你能调查一下吗,也许我们会有一些线索。
      • 我找到了一个解决方案,并发布了,可能你想检查一下。
      猜你喜欢
      • 2020-07-04
      • 1970-01-01
      • 2021-09-08
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多