Laravel资源和相关表

Laravel资源和相关表

问题描述:

I am getting my posts with resources and with this posts I also collect their categories but the categories come with full data, I need to limit returned data of categories,

Sample

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class PostResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'url' => $this->slug,
            'body' => $this->body,
            'user' => $this->user,
            'categories' => $this->categories,
            'created_at' => (string) $this->created_at,
            'updated_at' => (string) $this->updated_at,
        ];
    }
}

Code above return data like:

0   
id  75
title   "Fpost title"
url "post-slug"
body    "post_body"
user    {…}
categories  
 0  
  id    1
  title "xx"
  slug  "xx"
  photo "xx"
  meta_tags "xx"
  meta_description  "xx"
  status    "xx"
  created_at    "xx"
  updated_at    "xx"
  pivot {…}
 1  
  id    9
  title "xx"
  slug  "xx"
  photo "xx"
  meta_tags "xx"
  meta_description  "xx"
  status    "xx"
  created_at    "xx"
  updated_at    "xx"
  pivot {…}
created_at  "xx"
updated_at  "xx"

Form categories all I need is Title & Slug.

Question

How do I limit categories data to title and slug only?

我收到我的帖子资源和帖子我也收集他们的类别,但类别附带完整的数据, 我需要限制返回的类别数据, p>

Sample h1>
 &lt;?php 
 
namespace App \ Http \ Resources; \  n 
use Illuminate \ Http \ Resources \ Json \ JsonResource; 
 
class PostResource扩展JsonResource 
 {
公共函数toArray($ request)
 {
 return [
'id'=&gt;  $ this-&gt; id,
'title'=&gt;  $ this-&gt; title,
'url'=&gt;  $ this-&gt; slug,
'body'=&gt;  $ this-&gt; body,
'user'=&gt;  $ this-&gt; user,
'categories'=&gt;  $ this-&gt; categories,
'created'at'=&gt;  (字符串)$ this-&gt; created_at,
'reated_at'=&gt;  (字符串)$ this-&gt; updated_at,
]; 
} 
} 
  code>  pre> 
 
 

以上代码返回数据,如: p> \ n

  0 
id 75 
title“Fpost title”
url“post-slug”
body“post_body”
user {...} 
categories 
 0 
 id 1 
 title“xx”\  n slug“xx”
 photo“xx”
 meta_tags“xx”
 meta_description“xx”
 status“xx”
 created_at“xx”
 updated_at“xx”
 pivot {...} 
 1  
 id 9 
标题“xx”
 slug“xx”
 photo“xx”
 meta_tags“xx”
 meta_description“xx”
 status“xx”
 created_at“xx”
 updated_at“  xx“
 pivot {...} 
created_at”xx“
updated_at”xx“
  code>  pre> 
 
 

我需要的表单类别是 Title code>&amp; ; Slug code>。 p>

问题 h1>

如何将类别数据限制为 title code>和 slug code>只? p> div>

Try the below code, I made some changes in your code,

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class PostResource extends JsonResource
{
   public function toArray($request)
   {
      return [
        'id' => $this->id,
        'title' => $this->title,
        'url' => $this->slug,
        'body' => $this->body,
        'user' => $this->user,
        'categories' => CategoryResource::collection($this->whenLoaded('categories')),
        'created_at' => (string) $this->created_at,
        'updated_at' => (string) $this->updated_at,
      ];
   }
}

Note: add your CategoryResource depends on your relation.

and create a resource for Category CategoryResource

class CategoryResource extends JsonResource
{     
  public function toArray($request)
   {
      return [
        'title' => $this->title,
        'url' => $this->slug,
      ];
   }
}