Php Laravel API仅获取带有帖子的类别
我有API资源,我在我的API控制器中调用它,所以它会返回我的数据,我需要在这个API中添加一个额外的过滤器,但我不确定如何添加 我需要什么Php Laravel API仅获取带有帖子的类别,php,laravel,api,Php,Laravel,Api,我有API资源,我在我的API控制器中调用它,所以它会返回我的数据,我需要在这个API中添加一个额外的过滤器,但我不确定如何添加 我需要什么 获取仅包含post的类别 排除未发布结果的类别 我所拥有的 所有类别的名单,有和没有职位 代码 资源 <?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; use App\Http\Resources\PostResource;
资源
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\PostResource;
class CategoryResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'image' => url('images', $this->photo),
'url' => $this->slug,
'posts' => PostResource::collection($this->whenLoaded('posts')),
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
];
}
}
知道如何在我的控制器中添加该过滤器吗?您可以使用
has
功能过滤至少有一篇文章的类别,如下所示:
Category::where('status', '1')->with('posts')->has('posts')->orderby('id', 'desc')->get();
您可以使用
has
函数筛选至少有一篇文章的类别,如下所示:
Category::where('status', '1')->with('posts')->has('posts')->orderby('id', 'desc')->get();