【问题标题】:Hide fields from voyager Translatable对 voyager 隐藏字段
【发布时间】:2019-12-20 22:43:54
【问题描述】:

我想让一些字段从 json 响应中隐藏。 我使用了 Voyager 多语言功能。所以我的回复如下:

$collection = Diet::all()->makeHidden(['description'])->translate(app()->getLocale(), 'en');
return response()->json($collection);

但响应中包含描述字段。没有->translate(app()->getLocale(), 'en'),它也能完美运行。如何隐藏描述字段?

【问题讨论】:

    标签: php laravel voyager


    【解决方案1】:

    为了隐藏这些字段,我创建了 JsonResource,如下所示:

    <?php
    
    namespace App\Http\Resources;
    
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class DietCollection extends JsonResource
    {
        /**
         * Transform the resource collection into an array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function toArray($request)
        { 
            //here we return only required fields
            return [
                'id' => $this->id,
                'title' => $this->title,
                'image' => $this->image,
            ];
        }
    }
    

    并像这样使用它:

    $collection = Diet::all()->translate(app()->getLocale(), 'en');
    return response()->json(DietCollection::collection($collection));
    

    【讨论】:

      猜你喜欢
      • 2019-06-11
      • 2012-04-25
      • 1970-01-01
      • 2011-01-02
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多